MAUI navigation with parameters in MVVM-setting

123 Views Asked by At

Moving from MVVM design in Xamarin.Forms app with Prism, to MAUI-app based on Shell (and hopefully MVVM design as well), without Prism.

Prism offers the INavigationAware interface, where the method OnNavigatedTo gets invoked when the page is navigated to. The OnNavigatedTo methods takes a NavgationParameters object, making passing objects between pages simple and intuitive.

Moving to MAUI and Shell, I became aware of the QueryProperty annotation

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-8.0#pass-data

which takes care of the parameters, but still I miss the OnNavigatedTo method. In this method, besides parsing parameters, we could also do, for instance, backend calls to fetch data.

In order to mimic the OnNavigatedTo, my current approach is to do this in the code-behind (I use DI to instantiate MyPageViewModel)

public partial class MyPage : ContentPage
{
    public MyPage(MyPageViewModel viewModel) 
    {
        InitializeComponent();
        BindingContext = viewModel;
        NavigationPage.SetHasNavigationBar(this, false);
    }

    protected override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        ((MyPageViewModel)BindingContext).OnNavigatedTo(args);
        base.OnNavigatedTo(args);
    }

    protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
    {
        ((MyPageViewModel)BindingContext).OnNavigatedFrom(args);
        base.OnNavigatedFrom(args);
    }
}

I would have to insert this code into every code-behind. However, the NavigatedFromEventArgs do not contain the parameters parsed from the navigated-from page. For instance this navigation

await Shell.Current.GoToAsync($"{nameof(MyPage)}?TestModeKey=True", new Dictionary<string, object>
{
    ["MyKey"] = Myobject
});

Going back to https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-8.0#pass-data one could implement ApplyQueryAttributes, however, to make backend calls, do not not seem like the purpose if this method, nor is it async

public class MonkeyDetailViewModel : IQueryAttributable, INotifyPropertyChanged
{
    public Animal Monkey { get; private set; }

    public void ApplyQueryAttributes(IDictionary<string, object> query)
    {
        Monkey = query["Monkey"] as Animal;
        OnPropertyChanged("Monkey");
    }
    ...
} 

The current design in my Xamarin.Forms app is to load and fetch data for a view in the OnNavigatedTo-method, when the view is loaded. That could be probably be re-written to fetch the data before the navigation, but that would take a lot of test and effort.

How can the OnNavigated from Prism be mimicked with MAUI and Shell?

1

There are 1 best solutions below

2
On

Moving from MVVM design in Xamarin.Forms app with Prism, to MAUI-app based on Shell (and hopefully MVVM design as well), without Prism.

How can the OnNavigated from Prism be mimicked with MAUI and Shell?

Use Prism.Maui? It is provided by PrismLibrary. In addition, 9.0.271-pre and 9.0.401-pre(the latest version) do support .NET 8.