[Prism] Composite Application Guidance for WPF (4) -- bootstrapper
Zhou yinhui
By default, WPFProgramSpecify starturi In The XAML of the startup method app. Then the IDE will automatically generate a main method for us, and then generate a new window specified in starturi as the main window of the application, but we changed this method in Composite Application Guidance for WPF (3) -- create the first composite WPF application (if you do not know the prism startup method, we recommend you read it:
Public app ()
{
VaR boot = new bootstrapper ();
Boot. Run ();
}
Bootstrapper is similar to the following type:
Class bootstrapper: unitybootstrapper
{
Protected override dependencyobject createshell ()
{
VaR shell = new shell ();
Shell. Show ();
Return shell;
}
Protected override imoduleenumerator getmoduleenumerator ()
{
VaR configstory = new configurationstore ();
VaR enumerator = new configurationmoduleenumerator (configstory );
Return enumerator;
}
}
The shell here is essentially the main window of our application. It can be seen that it shows the main window in the createshell () method. Why, let's take a look at the bootstrapper in prism.
1. What is bootstrapper?
In prism, bootstrapper is the initiator of an application. Its role is to allow you to better control the Startup Process of the application. for example, Loading modules, Loading modules, and registering services. by default, the unitybootstrapper of Prism has completed a lot of work for you, such as dependency injection container creation, Region ing, and initialization module, we only need to specify the shell and the imoduleenumerator. But a good thing is that many methods in bootstrapper are virtual (including abstract methods ), we can rewrite these methods to change the startup content and startup logic.
2. What does bootstrapper do?
This is the help document of prism. We can see that it has completed 4 main tasks: Configuring the dependency injection container, configuring the region ing, creating the shell, initializing the module, and opening the prismSource codeIt is clearer (in the following prism SourceCode, I deleted some code that may interfere with the line of sight ):
Public void run (bool usedefaultconfiguration)
{< br> _ usedefaultconfiguration = usedefaultconfiguration;
// create a logger
iloggerfacade logger = loggerfacade;
// create a default container
container = createcontainer ();
// configure the container
configurecontainer ();
// Configure region adapter ing
Assumeregionadaptermappings ();
// Create a shell
Dependencyobject shell = createshell ();
If (shell! = NULL)
{
Regionmanager. setregionmanager (shell, container. Resolve <iregionmanager> ());
}
// Initialization module
Initializemodules ();
}
2.1 create a logger
Prism comes with a logger. Its principle is very simple, and trace is used:
Public class tracelogger: iloggerfacade
{
/// <Summary>
/// Record a new Log Based on the specified category and priority
/// </Summary>
/// <Param name = "message"> Log message </param>
/// <Param name = "category"> record type </param>
/// <Param name = "Priority"> priority of the record </param>
Public void log (string message, category, priority)
{
If (Category = category. Exception)
{
Trace. traceerror (Message );
}
Else
{
Trace. traceinformation (Message );
}
}
}
It can be seen everywhere in Prism
2.2. Create a default container
The dependency injection container plays the most important role in Prism (including other Compsite application frameworks, such as cab), because we need it for dependency injection (about dependency injection, for details, refer to [convert] dependency Injection & Control reverse IOC container and dependency injection mode (Chinese version ))
In this process, Prism mainly completes some basic components and service registration.
Protected virtual void configurecontainer ()
{
// Register a log instance with the container
Container. registerinstance (loggerfacade );
// Register yourself as a container
Container. registerinstance (container );
// Add an extension to the container to check whether the specified type has been registered in the container
Container. addnewextension <unitybootstrapperextension> ();
// Module enumerator
Imoduleenumerator moduleenumerator = getmoduleenumerator ();
If (moduleenumerator! = NULL)
{
// Register the module enumerator with the container
Container. registerinstance (moduleenumerator );
}
// If the default configuration is used, register the Cal basic service with the container.
If (_ usedefaultconfiguration)
{
Registertypeifmissing (typeof (icontainerfacade), typeof (unitycontaineradapter), true );
Registertypeifmissing (typeof (ieventaggregator), typeof (eventaggregator), true );
Registertypeifmissing (typeof (regionadaptermappings), typeof (regionadaptermappings), true );
Registertypeifmissing (typeof (iregionmanager), typeof (regionmanager), true );
Registertypeifmissing (typeof (imoduleloader), typeof (moduleloader), true );
}
}
2.3 configure region adapter ing
We know that region, as a placeholder, can be used as a view container. Which control types have this function? At least we know contentcontrol, itemscontrol, and so on. In fact, as long as there is a corresponding region adapter, and "Configure region adapter ing" is to associate the control type that can be used as a container with the corresponding adapter.
By default, Prism provides three adapters, which correspond to three container control types: selector, itemscontrol, contentcontrol, the three control types and their subtypes can be used as region containers.
Protected virtual regionadaptermappings extends eregionadaptermappings ()
{
VaR regionadaptermappings = container. tryresolve <regionadaptermappings> ();
If (regionadaptermappings! = NULL)
{
// Three region adapters provided by Cal by default
Regionadaptermappings. registermapping (typeof (selector), new selectorregionadapter ());
Regionadaptermappings. registermapping (typeof (itemscontrol), new itemscontrolregionadapter ());
Regionadaptermappings. registermapping (typeof (contentcontrol), new contentcontrolregionadapter ());
}
Return regionadaptermappings;
}
If we want to create a new region container type, we need to create a corresponding xxxregionadapter (inherited from regionadapterbase <t> class) for this type ), then rewrite the assumeregionadaptermappings () method and register the container type and the adapter.
2.4 create Shell
In the default bootstrapper unitybootstrapper, createshell is an abstract method. Therefore, you must define the shell creation process by yourself. Generally, it is very easy to initialize your shell and return it:
Protected override dependencyobject createshell ()
{
VaR shell = new shell ();
Shell. Show ();
Return shell;
}
2.5 initialization module
For module loading, Prism provides several methods. One is static reference loading (the same as common assembly references ), the second is dynamic loading (which can be divided into scanning specified folders and reading configuration files). Different loading methods have different module loaders, in the "initialization module" step, the most basic thing is to get the module loader, and then get the modules that need to be loaded when the reference program starts, and load them in:
Protected virtual void initializemodules ()
{
VaR moduleenumerator = container. tryresolve <imoduleenumerator> ();
VaR moduleloader = container. tryresolve <imoduleloader> ();
Moduleinfo [] moduleinfo = moduleenumerator. getstartuploadedmodules ();
Moduleloader. initialize (moduleinfo );
}
For more information about the module loader, see the following section.
OK. I have written so much today. Thank you very much for your great weekend.