Created
December 8, 2021 12:50
-
-
Save pictos/d5ff74f8c1571691bb19424475801eed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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