Git Product home page Git Product logo

xamarinhosting's Introduction

This archive is not actively maintained and has become outdated, I will try to update when I am able. Please also see the following reasources regarding using a Generic Host in Xamarin Forms.

Add ASP.NET Core's Dependency Injection into Xamarin Apps with HostBuilder

Another example from James

Xamarin.Forms Generic Host

Build status

A Xamarin.Forms generic host implementation for Microsoft.Extensions.Hosting.

Installation

You can add this library to your project using NuGet.

Package Manager Console Run the following command in the “Package Manager Console”:

PM> Install-Package West.Extensions.XamarinHosting

Visual Studio Right click to your project in Visual Studio, choose “Manage NuGet Packages” and search for ‘West.Extensions.XamarinHosting’ and click ‘Install’.

.NET Core Command Line Interface Run the following command from your favorite shell or terminal:

dotnet add package West.Extensions.XamarinHosting

Usage

Create a new Xamarin.Forms project and modify the following files to look like the examples below:

App.xaml.cs

public partial class App : Application
{
    public App() => InitializeComponent();

    public App(IHost host) : this() => Host = host;

    public static IHost Host { get; private set; }

    public static IHostBuilder BuildHost() => 
        XamarinHost.CreateDefaultBuilder<App>()
        .ConfigureServices((context, services) => 
        {
            services.AddScoped<MainPage>();
        });

    protected override void OnStart()
    {
        Task.Run(async () => await Host.StartAsync());
        MainPage = Host.Services.GetRequiredService<MainPage>();
    }

    protected override void OnSleep()
    {
        Task.Run(async () => await Host.SleepAsync());
    }

    protected override void OnResume()
    {
        Task.Run(async () => await Host.ResumeAsync());
    }
}

Android MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    ...

    // Android requires that we set content root.
    var host = App.BuildHost()
        .UseContentRoot(System.Environment.GetFolderPath(
            System.Environment.SpecialFolder.Personal)).Build();

    var application = host.Services.GetRequiredService<App>();

    LoadApplication(application);

    ...
}

iOS AppDelegate.cs

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    ...

    var host = App.BuildHost().Build();

    var application = host.Services.GetRequiredService<App>();

    LoadApplication(application);

    ...
}

Documentation

Docs are a work in progress, they can be found here.

xamarinhosting's People

Contributors

jamiewest avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xamarinhosting's Issues

First Issue, an introduction

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.