I'm currently working on refactoring our mobile app from Xamarin to MAUI. In the Xamarin app, we use Prism for navigation, but we want to stop using that. For navigation, we followed Max Borrmanns approach here for triggering the OnAppearing() function once moving to a page: https://learn.microsoft.com/en-us/answers/questions/1186365/net-maui-viewmodel-alternative-to-onappearing
In Prism, they have a function called OnNavigatedTo(), which functions similarly, but also takes INavigationParameters as input. so in Prism, when a page is being navigated to, the following is called:
public async void OnNavigatedTo(INavigationParameters parameters)
OnNavigatedTo() is called and we don't have to explicitly define any parameters, they are just kind of available when its called. As a replacement I was thinking of doing something like this:
public async void OnAppearing(Dictionary<String, Object> parameters)
The problem with this approach is that the parameters have to be manually defined, which means we aren't actually using any navigation parameters, since we keep making new ones. Is there a native MAUI way of doing this?
Shell's
GoToAsync()provides the possibility to pass anIDictionary<string, object>as can be seen in the documentation.Your ViewModel then needs to implement
IQueryAttributable. This would be the "native" MAUI way using Shell, but you still need to explicitly provide the parameters, I'm not aware of some implicit mechanism.Page:
ViewModel:
Register route for SomePage:
Navigation:
You could also create an abstract BaseViewModel so that your ViewModels only need to inherit from that and override
ApplyAttributes():