It it works with:
- .NET MAUI
- Microsoft.Extensions.DependencyInjection
- MAUI NavigatonPage.
- MAUI FlyoutPage (check the samples to see how it works).
using Naveasy;
- Inside this
ViewModelask for an instance ofINavigationServiceon it'sclassconstructor and store it in a private field. - Make this
PageViewModelto implementNaveasy.IPageLifecycleAware - Inside the method
void OnAppearing()of this VM use theINavigationServiceinstance that you've got to navigate to other page of your choice. - Tip.: You can use this
StartupPageViewModeto implement custom logic like quering you web API's or checking credential and ect conditionaly navigate to login page or another page if the user is alrealy logged-in. - in example below I've named it
StartupPage&StartupPageViewModel
- Call the generic method
.UseNaveasy<TViewModel>();using yourStartupPageViewModeltype. - Pay attention that you should specify here the Type of your startup page
ViewModelNOT thePageok, cause Naveasy is aViewModeltoViewModelnavigation framework. - The same concept explained above must be followed when you'll use the
INavigationServiceon your other Pages, use theViewModeltype rather the thePagetype to perform navigation. - Register your
Pageand it's correspondingPageViewModelonbuilder.Servicesusing Navaeasy's.AddTransientForNavigation<TPage, TPageViewModel>like described bellow.
using Naveasy.Core;
namespace Naveasy.Samples;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder()
.UseMauiApp<App>()
//The generic type specified in the line bellow (i.e: StartupPageViewModel) will be used to create a new window and navigate to it.
.UseNaveasy<StartupPageViewModel>()
.ConfigureFonts(fonts =>
{ fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });
//Register your types here using Microsoft.Extensions.DependecyInjection's container
builder.Services.
.AddTransientForNavigation<StartupPage, StartupPageViewModel>()
.AddTransientForNavigation<LoginPage, LoginPageViewModel>();
return builder.Build();
}
}- Naveasy v3 already does it for you internally, that's because we need to hook up our own events to make Naveasy work properly.
- If you have the
protected override Window CreateWindow(IActivationState? activationState)method on your App.xaml.cs, go there and remove it.- any custom logic that you might already have there if you were using Naveasy older then v3, can be moved to the
ViewModelcreated on step#3of this documentation.
- any custom logic that you might already have there if you were using Naveasy older then v3, can be moved to the
- Your App.xaml.cs file can be as clean as in the example bellow so you'll probably never have to look back to it in the future.
public partial class App : Application
{
public App()
{
InitializeComponent();
}
}- Naveasy already does this for you when it detects there will be the need for one.
You can refer to the sample that we have here on this repo to have a more in depth understanding of how you can navigate from one page to another and also how to handle the various page lifecycle events. You can optionaly implement the following handy base class which provides the various page lifecicle events that you would care about:
public class ViewModelBase : BindableBase, IInitialize, IInitializeAsync, INavigatedAware, IDisposable
{
public virtual void OnInitialize(INavigationParameters parameters)
{
}
public virtual Task OnInitializeAsync(INavigationParameters parameters)
{
return Task.CompletedTask;
}
public virtual void OnNavigatedFrom(INavigationParameters navigationParameters)
{
}
public virtual void OnNavigatedTo(INavigationParameters navigationParameters)
{
}
public virtual void Dispose()
{
}
}Fell free to contribute.
There's No support for MAUI AppShell until MSFT trully fixes the following issues: dotnet/maui#7354 dotnet/maui#21814 dotnet/maui#21816
This library was inpired on the Dotnet Foundation version of PRISM that's no longer a free library.