Prism4 document translation (chapter 2)

Source: Internet
Author: User
ArticleDirectory
    • The figure shows the magic horse is so cool .. It's too painful. If you're all lazy, let's take a look. You need to refer to the original document as needed .. Code .. Let's just take a look. I have already expressed the meaning of the important figure... Cough, appendix H, my poor Appendix H. Why are you so miserable... I regret it. I hate it .. Do not clear hidden files when they are not stored .. Throughout chapter 2, I have stored all the way! You can't afford to hurt a computer with a sudden power outage ). If you have any translation errors, you are welcome to criticize me and I will change it!
    • Chapter 2 initialization of the prism Application
As shown in the figure, the magic horse is too painful, Code Shenma .. It's too painful. If you're all lazy, let's take a look. You need to refer to the original document as needed .. Code .. Let's just take a look. I have already expressed the meaning of the important figure... Cough, appendix H, my poor Appendix H. Why are you so miserable... I regret it. I hate it .. Do not clear hidden files when they are not stored .. Throughout chapter 2, I have stored all the way! You can't afford to hurt a computer with a sudden power outage ). If you have any translation errors, you are welcome to criticize me and I will change it!

In addition, I would like to ask for your comments. Do you want to publish this item as I did in Chapter 1? In fact, I feel that it is difficult to get it together for a long time, if they are separated, you can easily locate the desired content. However, if you want to separate them, it would be too painful to watch them together. Please give me some comments.

According to my opinion on the first floor, I will assign a picture from this chapter, and the previous picture will not be completed. In addition, the empty line of the Code is temporarily unavailable .. I have no other editors, so I am sorry .. In addition, because I copied the word directly to Windows Live, it seems that the format will not be seen and I don't know how to solve it .. I have never studied it.

You can find the original article in the following locations:

Http://compositewpf.codeplex.com/releases/view/55580

Chapter 2 prism Application Program Initialization

This chapter describes how a prism application starts and runs. The prism application must be configured and registered during startup. This process is also called application startup guide.

2.1 What is bootstrapper

Bootstrapper is a class that initializes the prism application. With bootstrapper, you can control how components in the prism library are connected to your applications.

The prism library contains an inherited bootstrapper abstract class, which can be used with any window. Most methods of this class are virtual methods. You can rewrite these methods as needed.

(It is too annoying to send a picture during the startup process. The typing instructions are as follows: Create loggerfacade, create and configure the module list, create and configure the container, configure the region adapter, and configure the default region behavior, register framework exception types, create shell, initialize shell, and initialize various modules .)

The prism Library provides some classes inherited by boostrapper, which provide the implementation applicable to most applications. Only initialization and shell creation can be implemented by yourself.

2.1.1 dependency Injection

The prism application requires a dependency injection container. This library provides containers that work under the Unity (Unity Application Block) and MEF (managed extensibility framework) frameworks. Of course, you can also use your own defined windows. Some of the content in the boot guide has a configuration window and a type of registration to the container.

Prism library inclusionUnitybootstrapperAndMefbootstrapperThese two classes implement most of the functions required when unity or MEF is used as the dependency injection framework. Different startup genie may also add different steps to the startup process shown in.

2.1.2 create Shell

In traditional WPF applications, the URI of the startup window is defined in the app. XAML file. In a Silverlight application, it is specified by the rootvisual in the app. XAML code.

However, in the prism application, bootstrapper is responsible for creating the shell or startup window. That is because shell depends on some services, such as the Regional Manager, which must be configured before the shell display.

2.2 key content

When you decide to use Prism in your application, you must make the following decisions:

L decide to use unity, MEF, or other custom dependency injection frameworks. This determines which kind of bootstrapper you will use or a custom bootstrapper.

L consider some special services used in your program because they need to be registered in the container.

L determine what type of Log Service to use, such as using the built-in Log service, or creating one by yourself.

L determine the module startup sequence: through detailed code descriptions, automatic detection, configuration files, or attributes in XAML.

The following sections in this chapter describe the above points.

2.3 Core scenarios

Creating the startup sequence is an important scenario for you to create an application. This section describes how to create a shell, configure the dependency injection container, register application-level services, and read and initialize modules.

2.3.1 create bootstrapper

If you select the unity or MEF framework, it is very easy to create a simple bootstrapper. You only need to createUnitybootstrapperOrMefbootstrapperAnd implementCreateshellMethod. If you have special requirements, rewrite them.InitializeshellMethod.

2.3.1.1 implement the createshell Method

The createshell method is used to specify the top-level window of the prism application for developers. Shell usually refersMainwindowOrMainpage. You need to return a shell instance to implement this method. In a prism application, you can specify a shell or container as needed.

The following instances useServicelocatorClass specifies the shell object.

Protected override dependencyobject createshell ()

{

Return servicelocator. Current. getinstance <shell> ();

}

[Note]: You can often see the use in dependency injection containers.ServicelocatorInstantiate objects instead of directly instantiating objects.ServicelocatorIs implemented by specific containers, which is a good way to decouple containers from other code. Of course you do not useServicelocatorInstead, it is directly referenced.

2.3.1.2 implement the initializeshell Method

When you create a shell, you need to do some necessary steps before the display. Based on your use of WPF or Silverlight,InitializeshellThere are multiple implementation methods. In Silverlight, you need to setRootvisual.

Protected override void initializeshell ()

{

Application. Current. rootvisual = shell;

}

If you are using WPF, you need to create a shell object and assign itMainwindow.

Protected override void initializeshell ()

{

Application. Current. mainwindow = shell;

Application. Current. mainwindow. Show ();

}

Base ClassInitializeshellIs empty, or is it better not to call.

2.3.2 create and configure a module list

If you create a modular application, you need to create and configure a list of modules. Prism passedImodulecatalogTo determine which modules are useful to the application, which modules need to be downloaded, and where to put them.

BootstrapperProvidesModulecatalogTo indicate the list, it is in the virtual MethodCreatemodulecatalog. In the implementation of the base class, it returns an emptyModulecatalogOf course, this method can also be overloaded to provide a different imodulecatalog instance. As shown below, this is in the modularity with MEF for Silverlight Quickstart codeQuickstartbootstrapper.

Protected override imodulecatalog createmodulecatalog ()

{

// When using MEF, the existing prism modulecatalog is still

// The place to configure modules via configuration files.

Return modulecatalog. createfromxaml (New uri (

"/Modularitywithmef. Silverlight; component/modulescatalog. XAML ",

Urikind. Relative ));

}

Whether inUnitybootstrapperStill inMefbootstrapperClass, the run method is calledCreatemodulecatelogMethod, and use the return value to initializeModulecatelogAttribute. If you override this method, you do not need to call the base class implementation because your code already provides those functions. For more information, see Chapter 4 "modular application development ".

2.3.3 create and configure containers

Containers are very important roles in Prism applications. Both the prism library and applications are based on the dependency injection framework. During container configuration, only a few core services are registered. In addition to these services, you can define some services to provide other functions required by applications.

2.3.3.1 core services

Service Interface

Description

Imodulemanager

Defines the service interfaces of the retrieval and initialization modules.

Imodulecatalog

Contains the metadata of the module. The prism Library provides several lists.

Imoduleinitializer

Initialize the module.

Iregionmanager

The registration and retrieval area is the visible part of the layout.

Ieventaggregator

A collection of events that describe the coupling between publishers and subscribers.

Iloggerfacade

The encapsulation of a log mechanism can also be used to customize a log system. The stock trader reference implementation (stock trader RI) uses the Enterprise Library logging application blockEnterpriselibraryloggeradapterClass to demonstrate how to use a custom log system. Log service, that isCreateloggerThe return valueRunThe method is registered with the window. BesidesCreateloggerMethod. The log service registered by other methods is invalid.

Iservicelocator

Allow the prism library to reference the container. If you need a custom or extended library, it will be very useful.

2.3.3.2 special application services

The following services are used in stock trader Ri. This example describes the services that may be used in a program.

stock trader RI services

description

imarketfeedservice

provides simulated real-time market data. positionsummarypresentationmodel updates screen data without the special reminder of this service.

imarkethistoryservice

provides the historical data required to display the trend chart.

iaccountpositionservice

provides a list of funds in the folder.

iordersservice

Save the submitted sales/purchase orders.

inewsfeedservice

provides a list of selected news items.

iwatchlistservice

submitted when a new monitoring item is added.

UnitybootstrapperAndMefbootstrapperIs two provided by the prism LibraryBootstrapper. They use different methods to create and configure similar content. ,

2.3.4 create and configure containers in unitybootstrapper

UnitybootstrapperInCreatecontainerThe method returns only oneUnitycontainer. In most cases, you do not need to modify this method. Of course, this is a virtual method to ensure flexibility.

After creating a container, you must configure your container according to application requirements.UnitybootstraperImplemented inConfigurecontainerTo register several core services of the prism library, as shown below:

[Note]: This is a module that needs to beInitialzeMethod to register a module-level service.

Protected virtual void configurecontainer ()

{

...

If (usedefaultconfiguration)

{

Registertypeifmissing (typeof (iservicelocator), typeof (unityservicelocatoradapter), true );

Registertypeifmissing (typeof (imoduleinitializer), typeof (moduleinitializer), true );

Registertypeifmissing (typeof (imodulemanager), typeof (modulemanager), true );

Registertypeifmissing (typeof (regionadaptermappings), typeof (regionadaptermappings), true );

Registertypeifmissing (typeof (iregionmanager), typeof (regionmanager), true );

Registertypeifmissing (typeof (ieventaggregator), typeof (eventaggregator), true );

Registertypeifmissing (typeof (iregionviewregistry), typeof (regionviewregistry), true );

Registertypeifmissing (typeof (iregionbehaviorfactory), typeof (regionbehaviorfactory), true );

}

}

In the InitiatorRegistertypeifmissingMethod to ensure that a service is not registered twice. You can use the default service. If you do not need to use the default serviceBootstrapper.RunMethod. You can also rewrite your ownConfigurecontainerMethod To disable services you do not want, such as event aggregators.

[Note]: if you do not use the default configuration, you must manually register the required service.

If you want to extend the defaultConfigurecontainerMethod, you only need to rewrite it in your code and decide whether to call the method of the base class as needed. The following code is used in the modularity for WPF (with unity) Quickstart example.Quickstartbootstrapper. This implementation calls the configuration method of the base class and registers it with unity.ImoduletrackerImplementation classModuletrackerAnd oneCallbackloggerSingletonCallbacklogger.

Protected override void configurecontainer ()

{

Base. configurecontainer ();

This. registertypeifmissing (typeof (imoduletracker), typeof (moduletracker), true );

This. Container. registerinstance <callbacklogger> (this. callbacklogger );

}

2.3.5 create and configure containers in mefbootstrapper

MefbootstrapperClassCreatecontainerFirst, it createsAssemblycatelogAnd oneCatalogexportprovider.CatalogexportproviderAllowMefextesionsThe program provides some default exported content of the prism class. You can also reload them as needed. ThenCreatecontainerCreate a userCatalogexportproviderExport dataCompositioncontainerInstance. In most cases, you do not need to modify this method. Of course, this is a virtual method to ensure flexibility.

[Note]: In Silverlight, because of some security constraints, retrieving an assembly by type is not allowed. Prism providesAssembly. getcallingassemblyTo replace other methods.

After creating a container, you must configure your container according to application requirements.MefbootstraperImplemented inConfigurecontainerTo register several core services of the prism library, as shown below. Before rewriting this method, consider whether you need to call the methods of the base class to register these services or manually implement them.

Protected virtual void configurecontainer ()

{

This. registerbootstrapperprovidedtypes ();

}

Protected virtual void registerbootstrapperprovidedtypes ()

{

This. Container. composeexportedvalue <iloggerfacade> (this. Logger );

This. Container. composeexportedvalue <imodulecatalog> (this. modulecatalog );

This. Container. composeexportedvalue <iservicelocator> (New mefservicelocatoradapter (this. Container ));

This. Container. composeexportedvalue <aggregatecatalog> (this. aggregatecatalog );

}

Note:MefbootstraperThe core service is added to the container in the form of a separate instance, so that you can easily locate its position in the container in the entire application.

BesidesCreatecontainerAndConfigurecontainerMethod,MefbootstraperTwo other configurations are provided for configuring the MEF framework.AggregatecatalogAttribute method.CreateaggregatecatalogMethod returnsAggregatecatalog. And otherMefbootstrapperSame method,AggregatecatalogIt is also a virtual method that can be rewritten.

ConfigureaggregatecatalogThe method allows youAggregatecatalogAdd the registration type. For example, in the modularity with MEF for Silverlight Quickstart instanceQuickstartbootstrapperClear directionAggregatecatalogModulea and modulec are added, as shown below.

Protected override void configureaggregatecatalog ()

{

Base. configureaggregatecatalog ();

// Add this Assembly to export moduletracker

This. aggregatecatalog. Catalogs. Add (

New assemblycatalog (typeof (quickstartbootstrapper). Assembly ));

// Module A is referenced in the project and directly in code.

This. aggregatecatalog. Catalogs. Add (

New assemblycatalog (typeof (modulea. modulea). Assembly ));

// Module C is referenced in the project and directly in code.

This. aggregatecatalog. Catalogs. Add (

New assemblycatalog (typeof (modulec. modulec). Assembly ));

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.