Skip to content

Instantly share code, notes, and snippets.

@pictos
Created December 8, 2021 12:50
Show Gist options
  • Save pictos/d5ff74f8c1571691bb19424475801eed to your computer and use it in GitHub Desktop.
Save pictos/d5ff74f8c1571691bb19424475801eed to your computer and use it in GitHub Desktop.
static class ComicRouteFactory
{
public static RouteFactory Create<TPage, TViewModel>()
where TPage : Page, new()
where TViewModel : BaseViewModel
{
return new Factory<TPage, TViewModel>();
}
private class Factory<GPage, GViewModel> : RouteFactory
where GPage : Page, new()
where GViewModel : BaseViewModel
{
public override Element GetOrCreate()
{
var page = new GPage();
var vm = ServiceProvider.GetService<GViewModel>();
page.BindingContext = vm;
return page;
}
}
}
// Registering the route
void RegisterRoute()
{
Routing.RegisterRoute(nameof(ComicsViewModel), ComicRouteFactory.Create<ComicPage, ComicsViewModel>());
}
//Register ViewModel
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
// ...
builder.Services.AddSingleton<ComicService>();
builder.Services.AddTransient<ComicsViewModel>();
return builder.Build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment