The Prism Container Extensions provide various additional extensions making the Prism Container easier to use with Splat, IServiceCollection/IServiceProvider and in scenarios where you may require a Singleton container that may need to be initialized from Platform specific code prior to PrismApplication being created. Note that both the Prism.Container.Extensions and Prism.DryIoc.Extensions are platform agnostic meaning you can use them on WPF or Xamarin Forms.
You can add the MyGet CI feed to nuget by adding it as a source in Visual Studio:
https://www.myget.org/F/prism-plugins/api/v3/index.json
Package | NuGet | MyGet |
---|---|---|
Prism.Container.Extensions | ||
Prism.DryIoc.Extensions | ||
Prism.DryIoc.Forms.Extended | ||
Shiny.Prism.DryIoc |
The PrismContainerExtension can be initialized automatically and accessed by simply calling PrismContainerExtension.Current
. You can also create a new container with any of the following methods:
PrismContainerExtension.Create(new Container());
// OR
new PrismContainerExtension(new Container());
NOTE That by default the container extension will ensure that the underlying container is properly configured to work with Prism Applications.
When using the extended container extension you simply need to add the following to your PrismApplication to ensure that it uses the same instance that may have been created prior to the initialization of PrismApplication.
protected override IContainerExtension CreateContainerExtension() => PrismContainerExtension.Current;
NOTE: This section ONLY applies to applications that are based on the Official packages from Prism. If you're using an Extended PrismApplication from this repo you do not need to modify the PrismApplication.
The Prism.DryIoc.Forms.Extended package is designed to make it even easier for you to integrate these fantastic packages. As you'll see using it is identical in every way to creating a typical Prism Application. The only difference is that you are installing the Prism.DryIoc.Forms.Extended package instead of Prism.DryIoc.Forms.
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
x:Class="Contoso.Awesome.App">
</prism:PrismApplication>
public partial class App
{
protected override void OnInitialized()
{
InitializeComponent();
NavigationService.NavigateAsync("MainPage");
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register your services like normal
containerRegistry.RegisterForNavigation<MainPage>();
}
}
The extended PrismApplication is cross compiled for Xamarin.iOS and Xamarin.Android and provides several out of the box improvements over the normal PrismApplication.
- It includes the
ILogger
from Prism.Plugin.Logging - It has pre-wired support for logging XAML errors and other issues directly from Xamarin.Forms
- Becase it is cross compiled, it has global Exception Handling built in for:
- AndroidEnvironment
- ObjectiveC.Runtime
- AppDomain
- TaskScheduler
- It provides you all of the container extensions found here that help make advanced registration scenarios much easier.
<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
x:Class="Contoso.Awesome.App">
<prism:PrismApplication.ModuleCatalog>
<prism:ModuleCatalog>
<prism:ModuleInfo />
</prism.ModuleCatalog>
</prism:PrismApplication.ModuleCatalog>
</prism:PrismApplication>
Shiny uses the Microsoft.Extensions.DependencyInjection pattern found in ASP.NET Core applications with a Startup class. This in particular is a use case in which you will need to initialize a container prior to Forms.Init being called on the native platform. To work with Shiny you simply need to do something like the following:
public class PrismStartup : Startup
{
public override void ConfigureServices(IServiceCollection services)
{
// Register services with Shiny like:
services.UseGpsBackground<MyDelegate>();
}
public override IServiceProvider CreateServiceProvider(IServiceCollection services)
{
return PrismContainerExtension.Current.CreateServiceProvider(services);
}
}
With your App using the PrismApplication from Prism.DryIoc.Forms.Extended you now only need to reference the PrismStartup as the base class for your Startup class like:
public class MyStartup : PrismStartup
{
public override void ConfigureServices(IServiceCollection services)
{
// Register services with Shiny like:
services.UseGpsBackground<MyDelegate>();
}
}
You can now pass your startup to the ShinyHost at your application's startup and use full Dependency Injection of Shiny's services in your app, full DI of services from Prism's container within services that are resolved by Shiny.
NOTE: Shiny uses IServiceProvider
which does not support the use of named services.
// Android
public class App : Android.App.Application
{
public override void OnCreate()
{
AndroidShinyHost.Init(this, new MyStartup());
}
}
// iOS
[Register("AppDelegate")]
public partial class AppDelegate : FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
// this needs to be loaded before EVERYTHING
iOSShinyHost.Init(new MyStartup());
Forms.Init();
this.LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
// if you are using jobs, you need this
public override void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
=> JobManager.OnBackgroundFetch(completionHandler);
}