This is a gap in the .NET MAUI functionality. The lack of a true "OnStart/OnNavigatedTo/etc" really makes things like this difficult for users, so they put running code inside the constructor. This can cause several issues:
- Properties like IsBusy can be missed due to timing because the view isn't ready yet.
- If an error happens, sometimes devs like to show a dialog "Hey user, are http request timed out", but if they show this dialog before the view is ready - the app can crash.
3rd party libraries like https://prismlibrary.com/ cover this gap perfectly by mapping up the page lifecycle to an interface marked viewmodel.
In this case and for the sake of new users, I suggest adding a "virtual OnNavigatedTo()" to your baseviewmodel and having your pages call it using something like
public abstract class BaseContentPage : ContentPage
{
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
(BindingContext as BaseViewModel)?.OnNavigatedTo();
}
}
Happy to send a PR for this.
MauiWorkshop/src/MyNewsReader/ViewModels/NewsFeedListViewModel.cs
Line 17 in 155283e
This is a gap in the .NET MAUI functionality. The lack of a true "OnStart/OnNavigatedTo/etc" really makes things like this difficult for users, so they put running code inside the constructor. This can cause several issues:
3rd party libraries like https://prismlibrary.com/ cover this gap perfectly by mapping up the page lifecycle to an interface marked viewmodel.
In this case and for the sake of new users, I suggest adding a "virtual OnNavigatedTo()" to your baseviewmodel and having your pages call it using something like
Happy to send a PR for this.