InProgramTo use the framework, there must be a starting point. The framework will be initialized here to process the relevant configuration information. Bootstrapper plays this role in prism.
Prism provides an abstract base class bootstrapper. This class contains many empty virtual methods that can be rewritten to add their own logic. This base class has nothing to do with any container, so you can inherit it to implement bootstrapper based on the specific container, but we usually do not have to do this, prism provides two default container-based bootstrapper -- unitybootstrapper and mefbootstrapper, which use unity and MEF to implement dependency injection respectively. The job we need to do is to select a suitable class between the two classes. Just configure it a little bit. Of course, if you do not like these two containers or existing programs use other containers (such as spring. net, Castle, etc.), you can also inherit the boostrapper abstract base class to implement your springbootstrapper and castlebootstrapper. Although the unitybootstrapperCodeIt looks simple, but it is not that easy to implement castlebootstrapper (you can try it if you don't believe it), so a better way is to use the ready-made method.
So what does bootstrapper do?
1. Create a logger:
Execute the createlogger method. By default, an emptylogger is created and logs are not output anywhere. Of course, it can be expanded. For example, you can use clog as an adapter.
2. Create and configure modulecatalog
Run the createmodulecatalog method. By default, an empty modulecatalog is created. Then execute the configuremodulecatalog method. By default, this method is empty. You can override these two methods and add custom logic for obtaining modulecatalog. For example, you can read module information from a XAML file in createmodulecatalog.
Protected override imodulecatalog createmodulecatalog () {return modulecatalog. createfromxaml (New uri ("/assemblyname; component/modulescatalog. XAML", urikind. Relative ));}
3. Create and configure the dependency injection container
In prism, dependency injection is used to manage various components. You can use any container you are familiar with, such as castle and unity. Prism has built-in support for unity and MEF. Therefore, there are two predefined bootstrapper: unitybootstrapper and mefbootstrapper. unitycontainer and compositioncontainer are used as dependency injection containers respectively. Take unitybootstrapper as an example. In this step, the createbootiner method is called first, and an unitycontainer is returned. Then, the configurecontainer method is called. In this method, some common classes are registered to the container.
4. Configure the default region adapter ing
To enable the UI control in XAML to use region, You need to register it first. By default, Prism supports the following control types: tabcontrol, selector, itemscontrol, and contentcontrol. Of course, you can also implement the iregionadapter interface or directly inherit regionadapterbase <t> to make other controls also support region.
5. Configure the default region behavior (behavior)
Add some default actions for regionbehaviorfactory. In this way, the region behavior can be extended. You can implement the iregionbehavior interface or inherit regionbehavior to customize the region behavior, and rewrite the configuredefaultregionbehaviors method to add it to region.
6. Register framework exception types
Prism provides the exceptionextensions class to help developers locate root exceptions that occur. In this step, add a new root exception to predictionextensions by calling the registerframeworkexceptiontypes method.
7. Create and initialize Shell
First, call the createshell method to create a shell. This is an abstract method. Generally, this method returns the page as the whole website container. Regionmanager attach to shell, update the defined regions, and call the initializeshell method to initialize shell. By default, this is an empty method. You can rewrite this method to add custom logic. In this method, you can display the shell as the root container page of The Silverlight program.
Protected override void initializeshell () {application. Current. rootvisual = shell ;}
8. initialize modules
Calling the initializemodules method is actually calling the modulemanager. Run method. It will call the initialize method of all modules whose initializationmode is whenavailable.
So far, the entire container initialization process is complete.
Commonservicelocator is also a product of the patterns & Practices Group. Its function is very simple, that is, to uniformly rely on the interface of the injection container, so that the program does not have to rely on a specific container, just need to use servicelocator, and then indirectly use a variety of other containers. In prism, servicelocator is used for internal management. Therefore, no matter what containers are used, you must provide an adapter that implements the iservicelocator interface. For example, to use unity, you must provide the unityservicelocatoradapter and MEF to provide the mefservicelocatoradapter. No matter what external containers are used, internal changes are not required. Therefore, if you want to use Prism to start a program, it is a good choice to use servicelocator instead of relying on the injection container interface, in this way, you can easily replace the container as needed. You only need to override one bootstrapper and one servicelocatoradapter.