dotnet / docs-maui

This repository contains documentation for .NET Multi-platform App UI (MAUI).
https://docs.microsoft.com/dotnet/maui
Creative Commons Attribution 4.0 International
222 stars 194 forks source link

[Docs] Add docs about CarouselView changes in MAUI that doesn't allow you to bind a list of views as an ItemSource #2219

Open PureWeen opened 6 months ago

PureWeen commented 6 months ago

Description

We get a number of migration-compatibility issues from users updating from XF where they start getting the following exceptions.

https://github.com/dotnet/maui/issues/20852 https://github.com/dotnet/maui/issues/16577 https://github.com/dotnet/maui/issues/15920

XF did not enforce a one-to-one relationship with renderers. In XF, the CarouselView would continuously request new renderers for a single XF view, resulting in one XF view supporting multiple platform views. This likely was an oversight. While this approach worked well at times, it could lead to very confusing behavior when it failed, with odd ping ponging between different renderers referencing the same cross-platform view.

The correct approach is to employ a DataTemplateSelector that generates a new view type for each item requested by the CarouselView, or to use data binding to alter the view used for the data template. The data bound to the CarouselView shouldn't know about the view.

To make this all more confusing, on iOS when adding a UIView that already has a parent to a new parent it will automatically detach it from the former. So, on iOS it won't throw an exception, but it might cause flickering as views vanish. For consistency, an exception should probably have been thrown. There is no situation where an element should "magically" change parents. If an element already has a parent and is being added to another, it indicates a problem.

As a data-driven control designed to manage numerous views, the CarouselView performs best and most reliably when the data dictates the views, rather than attempting to insert custom views.

It would be useful to 1) improve the docs 2) possibly throw a better exception.

Vannoctar2 commented 6 months ago

I use a carousel view in which I initialize a contentview. This contentview contains a canvasview (skiasharp) which displays curves and graphs which represent the itemsource data.

It seems difficult to define this in a datatemplateselector.

PureWeen commented 6 months ago

I use a carousel view in which I initialize a contentview. This contentview contains a canvasview (skiasharp) which displays curves and graphs which represent the itemsource data.

It seems difficult to define this in a datatemplateselector.

Can you use bindings to modify contentview/canvasview?

that's the idea with data driven controls like the CV

You have the data and you have the templates. The two things are supposed to be somewhat separate.

When the data gets assigned to a view the view modifies itself based on the data via bindings and other mechanisms. The data set on the itemsource shouldn't really know anything about how it's displayed or about the view.

Vannoctar2 commented 6 months ago

I have updated my code. It's working, the application no crash.

Thank you very much! I think this topic can be useful for people using the MicroChart library.

stewartsims commented 3 months ago

So are you saying it is bad practice to bind something like ObservableCollection<ContentView> to ItemsSource on a CarouselView? And that the fact it worked in Xamarin Forms is not intentional, therefore any apps migrated from XF that use CarouselView must be rewritten to bind data rather than views otherwise they will experience errors like those listed?

If so a note to this effect in the documentation would definitely be helpful - perhaps tagging it with a warning for those carrying out XF to MAUI migration would be useful too.

daltzctr commented 1 month ago

I disagree with this

As a data-driven control designed to manage numerous views, the CarouselView performs best and most reliably when the data dictates the views, rather than attempting to insert custom views.

On mobile platforms, it's very common to want to achieve a carousel style of navigation. In-fact, I would argue this is one of the core styles of navigation on mobile platforms. This means that these pages contain arbitrary content, and are not data driven as you mentioned.

You could argue it fits the place for a different control, but that control does not exist and this has back broken Xamarin -> MAUI users since it's inception, encouraging hurt feelings and a "why does MS break stuff" sentiment.

On Android, this is implemented with a ViewPager where tabs natively support this swipe style of navigation. Because MAUI shell does a completely custom implementation, it does not support this. There is no way to support this. There are no alternatives.