Bootstrapper - canton7/Stylet GitHub Wiki
The bootstrapper is responsible for bootstrapping your application. It configures the IoC container, creates a new instance of your root ViewModel and displays it using the WindowManager
. It also provides various other functions, described below.
The bootstrapper comes in two flavours: BootstrapperBase<TRootViewModel>
, which requires you to configure the IoC container yourself, and Bootstrapper<TRootViewModel>
, which uses Stylet's built-in IoC container, StyletIoC.
Example bootstrapper, using StyletIoC:
C# | VB.NET |
class Bootstrapper : Bootstrapper<MyRootViewModel>
{
protected override void OnStart()
{
// This is called just after the application is started, but before the IoC container is set up.
// Set up things like logging, etc
}
protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
// Bind your own types. Concrete types are automatically self-bound.
builder.Bind<IMyInterface>().To<MyType>();
}
protected override void Configure()
{
// This is called after Stylet has created the IoC container, so this.Container exists, but before the
// Root ViewModel is launched.
// Configure your services, etc, in here
}
protected override void OnLaunch()
{
// This is called just after the root ViewModel has been launched
// Something like a version check that displays a dialog might be launched from here
}
protected override void OnExit(ExitEventArgs e)
{
// Called on Application.Exit
}
protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e)
{
// Called on Application.DispatcherUnhandledException
}
} |
Public Class Bootstrapper : Inherits Bootstrapper(Of MyRootModel)
Protected Overrides Sub OnStart()
' This Is called just after the application Is started, but before the IoC container Is set up.
' Set up things Like logging, etc
End Sub
Protected Overrides Sub ConfigureIoC(ByVal builder As IStyletIoCBuilder)
' Bind your own types. Concrete types are automatically self-bound.
builder.Bind(Of IMyInterface).To(Of MyType)()
End Sub
Protected Overrides Sub Configure()
' This Is called after Stylet has created the IoC container, so this.Container exists, but before the
' Root ViewModel Is launched.
' Configure your services, etc, in here
End Sub
Protected Overrides Sub OnLaunch()
' This Is called just after the root ViewModel has been launched
' Something Like a version check that displays a dialog might be launched from here
End Sub
Protected Overrides Sub OnExit(ByVal e As ExitEventArgs)
' Called on Application.Exit
End Sub
Protected Overrides Sub OnUnhandledException(ByVal e As DispatcherUnhandledExceptionEventArgs)
' Called on Application.DispatcherUnhandledException
End Sub
End Class |
Using another IoC container with Stylet is easy. I've included bootstrappers for a number of popular IoC containers in the Bootstrappers project. These are all unit-tested but not battle-tested: feel free to customize them.
Note that the Stylet nuget package / dll don't include these, as it would add unnecessary dependencies. Similarly, I don't publish IoC container-specific packages, as that's a waste of effort.
Copy the bootstrapper you want from the above link into your project somewhere. Then subclass it, as you would normally subclass Bootstrapper<TRootViewModel>
, documented above. Then add your subclass to your App.xaml.cs, as documented in Quick Start, e.g.
public class Bootstrapper : AutofacBootstrapper<ShellViewModel>
{
}
<Application x:Class="Stylet.Samples.Hello.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="https://github.com/canton7/Stylet"
xmlns:local="clr-namespace:Stylet.Samples.Hello">
<Application.Resources>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
</Application.Resources>
</Application>
If you want to write your own bootstrapper for another IoC container, that's easy too. Take a look at the bootstrappers above to see what you need to do.
s:ApplicationLoader is itself a ResourceDictionary. If you need to add your own resource dictionary to App.xaml, you will have to nest s:ApplicationLoader inside your ResourceDictionary as a MergedDictionary, like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<s:ApplicationLoader>
<s:ApplicationLoader.Bootstrapper>
<local:Bootstrapper/>
</s:ApplicationLoader.Bootstrapper>
</s:ApplicationLoader>
<ResourceDictionary Source="YourDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>