Cysharp / ObservableCollections

High performance observable collections and synchronized views, for WPF, Blazor, Unity.
MIT License
596 stars 45 forks source link

Support for .Net Maui #72

Closed pboisso closed 1 month ago

pboisso commented 1 month ago

Hi there,

Thanks for building this collection, which seems quite useful for what I'd like to achieve. I did test it in Maui apps based on the "XAML based UI platforms" instructions on the read-me, but Maui iOS does not behave so good (cells become blank on updates). Android is working just fine.

Tried with ToNotifyCollectionChanged, and ToNotifyCollectionChangedSlim. Also tried binding the collection directly. Same thing.

Using Application.Current.Dispatcher.DispatchAsync to invoke notification events.

I can't see what's wrong.

I saw no mention of Maui in the read me. Is anyone using it in Maui apps and if so, should it be used according to the XAML based plateform instructions?

Thanks.

neuecc commented 1 month ago

Thanks for trying it out. I'm not very familiar with MAUI, and even if I did try it out, I'd probably only be able to run it on Windows, so thanks for checking it on iOS. Could you please provide some more specific code for testing, such as how you applied DispatchAsync?

pboisso commented 1 month ago

Hi @neuecc, thanks for the answer.

You can develop for Maui both in Windows and Mac, but I'm referring to the end result platform. When the app is compiled for Android, it seems to be running fine, but when the application is compiled to run as an iOS app, it is not.

In both cases, I use this to dispatch events...

public class MauiDispatcherCollection() : ICollectionEventDispatcher
{
    public void Post(CollectionEventDispatcherEventArgs ev)
    {
        Application.Current.Dispatcher.DispatchAsync(() =>
        {
            // notify in dispatcher
            ev.Invoke();
        });
    }
}
GroupBySectionsToBind = _groupbysections.ToNotifyCollectionChanged(new MauiDispatcherCollection());

As said, tried with both regular and Slim. Same results. If you hear about anyone else who made it working for Maui, that would be great.

Thanks in advance for your help.

neuecc commented 1 month ago

I think this discussion is similar. I think Application.Current.Dispatcher is not the appropriate approach. https://github.com/dotnet/maui/issues/21452

pboisso commented 1 month ago

What would you suggest as the appropriate approach?

neuecc commented 1 month ago

I don't know. However, if this occurs with a normal ObservableCollection due to MAUI/iOS and binding issues, then it's not an issue with this library.

pboisso commented 1 month ago

ObservableCollection was doing just fine.

FYI, here is what did the trick for us so far, still not perfect. Does that gives any indication on what we may be doing wrong?

public class MauiDispatcherCollection() : ICollectionEventDispatcher
{
    public void Post(CollectionEventDispatcherEventArgs ev)
    {
        if (Application.Current.Dispatcher.IsDispatchRequired)
        {
            Application.Current.Dispatcher.DispatchAsync(() =>
            {
                // notify in dispatcher
                ev.Invoke();
            });
        }
        else    //already on main thread, run immediately
        {
            // notify in dispatcher
            ev.Invoke();
        }
    }
}