Search code examples
ios.netmvvmrefreshmaui

Refresh list via OnResume event in .NET MAUI


Regarding to a known .NET MAUI error in ScrollViewer & RefreshView, which is discussed about here, I am trying to find a workaround.

So I removed my RefreshView and now using just a CollectionView. The only function that is missing now is the "refresh-function" of my list, which I want to trigger now with the OnResume event in the App.xaml.cs which looks like that now:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        DependencyService.Register<Services.MockDataStore>();
        MainPage = new AppShell();      
    }

    protected override void OnResume()
    {
        base.OnResume();
        // method call should be implemented here
    }
}

Does anyone know how to access a method from my view model from that line of code? Thank you so much.


Solution

  • You could use the WeakReferenceManager for this.

    First, you define a class that serves as a type for the message:

    public class ResumeMessage {}
    

    Then, in your ViewModel or View (depending on where and how you would refresh the CollectionView), you could subscribe to the ResumeMessage like this:

    WeakReferenceMessenger.Default.Register<ResumeMessage>(this, (sender, args) =>
    {
        // here you can refresh the CollectionView
    });
    

    And in the OnResume() method of your App class, you can send the message that informs all subscribers that something should happen based on the ResumeMessage:

    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            DependencyService.Register<Services.MockDataStore>();
            MainPage = new AppShell();      
        }
    
        protected override void OnResume()
        {
            base.OnResume();
            WeakReferenceMessenger.Default.Send(new ResumeMessage());
        }
    }