Skip to content
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

Open
jamiewest opened this issue Mar 20, 2019 · 3 comments
Open

First Issue, an introduction #1

jamiewest opened this issue Mar 20, 2019 · 3 comments

Comments

@jamiewest
Copy link
Owner

So I thought I would open this up with a brief intro, this would normally go in the README or docs, but I'm still working on that. This repo contains code that extends the ASP.NET Core Generic Host located here for Xamarin.Forms applications. If you don't know what a Generic Host is then I encourage you to read about it on the docs page. I have for a long time wanted the ability to write Xamarin.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. The Generic 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 a Xamarin 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 to PR, add issues, and please please give feedback as I want to make this better.

Look forward to hearing from you soon!

@ElanHasson
Copy link

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?

@jamiewest
Copy link
Owner Author

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 OnStarted method that performs startup logic including setting the MainPage. However, a Hosted Service is registered as a Singleton in the Generic Host DI. This is problematic if you try and consume Scoped services from the pages you are navigating to (i.e. an EF Core DbContext). The solution I have been using is to use the ActivatorUtilities class from Microsoft.Extensions.DependencyInjection. This is a little bit similar to how Controllers are created using ASP.NET Core

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 BindingContext on the Page.

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 MasterDetailPage along with a NavigationPage.

@tmarkovski
Copy link

Great work @jamiewest! I'm glad I ran into this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants