-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First Issue, an introduction #1
Comments
This is cool, I've been looking for a way to do something similar. This assessment dependency injection to all content pages? Or just the main page? Do you still require static method calls to resolve in other pages? |
I've found that when navigating to pages you need to pay close attention to the scope of services you are consuming. For instance, I like to use a Hosted Service (see here) that registers an This will create the page and constructor inject any dependencies using a scoped service provider. var page = ActivatorUtilities.CreateInstance(App.Host.Services.CreateScope().ServiceProvider, typeof(HomePage)); Then we can make the view model. var viewModel = ActivatorUtilities.CreateInstance(App.Host.Services.CreateScope().ServiceProvider, typeof(HomePageViewModel)); And then you can set the page.BindingContext = viewModel;
App.Current.MainPage = page; I use a basic navigation service that was hacked a bit from the SmartHotel360 app. I register the navigation service as a Singleton with the DI and then consume it in my view models. public static IHostBuilder BuildHost() =>
XamarinHost.CreateDefaultBuilder<App>()
.ConfigureServices((context, services) =>
{
services.AddSingleton<INavigationService, NavigationService<MasterDetailPageType, NavigationPageType>>();
}); I can talk more about the navigation service if your interested, basically it uses a convention to try and resolve a page type for a given view model type based on names. If that doesn't work, if checks to see if you manually mapped a page type to a view model and then instantiate those. It also assumes you are using a |
Great work @jamiewest! I'm glad I ran into this repo. |
So I thought I would open this up with a brief intro, this would normally go in the
README
ordocs
, but I'm still working on that. This repo contains code that extends the ASP.NET CoreGeneric Host
located here forXamarin.Forms
applications. If you don't know what aGeneric Host
is then I encourage you to read about it on the docs page. I have for a long time wanted the ability to writeXamarin.Forms
application in a way that felt similar to ASP.NET Core web applications. I love the DI, Logging, Localization, and Configuration patterns and wanted this to be the core of my mobile apps story. TheGeneric Host
is the piece that brings all this together and I really enjoy using it and hope you will to.The code in this repo will hopefully demonstrate that the
Generic Host
works well in aXamarin
environment and doesn't get in the way of things. It doesn't replace the need hack around things, but it sure makes common infrastructure scenarios a lot less work. Please feel free toPR
, add issues, and please please give feedback as I want to make this better.Look forward to hearing from you soon!
The text was updated successfully, but these errors were encountered: