[Prism] Composite Application Guidance for WPF (3) -- create the first composite WPF Application

Source: Internet
Author: User
[Prism] Composite Application Guidance for WPF (3 )--Create the firstComposite WPF Application
Zhou yinhui

1.Prerequisites:

You need to downloadCal (Composite Application Library)Library is actually a fewDLLFile, which will be referenced in the project to build ourProgramYou can download them from here. Of course, you also needWPFApplicationIDEFor exampleVs2008.

2. CreateShell project

2.1InVSCreate a newWPF Application, SetCalAdd the library file to the project reference

2.2. The automatically generatedWindow1Rename the main window and its corresponding filesShell, Which will serve as ourComposite appOfShell.

2.3. CreateBootstrapper, Custom program Initialization Method

CreateBootstrapperClass to inherit fromUnity bootstrapperAnd rewrite some of the methods to implement our customization.

Internal class bootstrapper: unitybootstrapper

{

Protected override dependencyobject createshell ()

{

VaR shell = new shell ();

Shell. Show ();

Return shell;

}

Protected override imoduleenumerator getmoduleenumerator ()

{

Return new staticmoduleenumerator ();

}

}

By RewritingCreateshell ()Method specifies ourShell, By RewritingGetmoduleenumerator ()Method to specify the Loading Method of our module (Here we returnStaticmoduleenumeratorWill introduce how to use the configuration file to configure module loading)

2.4. Modify the program startup Method

We know that when you create a newWPF Application,VSTheApp. XAMLAutomatically generate specifiedStartupuriTo the main window, but this is not oursComposite WPF ApplicationWe needBootstrapperStart, So deleteApp. XAMLAutomatically generate the specifiedStartupuri, And rewriteAppClass constructor:

Public partial class app

{

Public app ()

{

VaR bootstrapper = new bootstrapper ();

Bootstrapper. Run ();

}

}

F5,ShellYou canRunGet up

3, CreateHello WorldModule

3.1. Add a new project"Helloworldmodule"(NowClasslibraryType)

3.2.AddCalLibrary to project reference, currently addedMicrosoft. Practices. CompositeAndMicrosoft. Practices. Composite. WPFYou can.

3.3In terms of syntaxModuleTo implementImoduleInterface,OK, CreateHelloworldmoduleClass:

Public class helloworldmodule: imodule

{

# Region imodule members

Public void initialize ()

{

System. Windows. MessageBox. Show ("this is Hello module ");

}

# Endregion

}

To avoid disturbing the reader's sight, we should simplify it.ModuleShowMessageBoxJustOK.

4, Load the moduleShell projectMedium

We can see thatShell projectAndHelloworld ProjectAre two completely independent projects. In actual development, they may be composed of two differentTeamDeveloped and tested independently. So in orderShellUsed inHelloword,CalMultiple methods are provided to discover and load modules. The simplest is static loading, that is, direct project reference. Here we use the accessory file to implement (CalYou can also scan folders to load these files)

4.1. Add configuration file

BackShell project, AddApplication configuration fileWhich generatesApp. configFile. In this file, we can configure the system module composition structure, loading sequence, and dependencies between modules:

<? XML version = "1.0" encoding = "UTF-8"?>

<Configuration>

<Configsections>

<Section name = "modules" type = "Microsoft. Practices. Composite. modularity. modulesconfigurationsection, Microsoft. Practices. Composite"/>

</Configsections>

<Modules>

<Module assemblyfile = "helloworldmodule. dll" moduletype = "helloworldmodule. helloworldmodule" modulename = "helloworldmodule">

</Module>

</Modules>

</Configuration>

4.2. Specify the Loading Method

Then return to ourBootstrapperClass, which specifies the loading mode of our modules as the configuration file:

Protected override imoduleenumerator getmoduleenumerator ()

{

VaR configstory = new configurationstore ();

Return new configurationmoduleenumerator (configstory );

}

4.3. Specify the module generation location

We know that the default generation position of the module is the project'sDebug(ReleaseDirectory, which causes a problem: OurShellThe project does not know the location of the files generated by these modules and thus cannot find the module files. Therefore, we can use the module project'sBuild EventsInPost-build event command lineTo automatically copy the generated fileShellProjectDebug(OrRelease) Directory:

Xcopy "$ (targetdir) *. *" $ (solutiondir) cag_helloworld \ bin \ $ (configurationname) "/y

(WhereCag_helloworldYesShellProject name)

Regenerate the project,F5,OK

5ViewInjectShellMedium

In the preceding exampleHellowordModule,MessageBoxNow let's see how to addView, AndShellShown in

5.1InShellAddRegion

BackShell. XAML, Add a container control asRegion, You canRegionAs a placeholder, it is used to place ourView

<Window X: class = "cag_helloworld.shell"

Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"

Xmlns: Cal = "http://www.codeplex.com/CompositeWPF"

Title = "shell" Height = "300" width = "300">

<Grid>

<Itemscontrol CAL: regionmanager. regionname = "mainregion"/>

</GRID>

</WINDOW>

5.2InHelloword ProjectJoinHelloworldview

AddUsercontrolToHelloword Project.UIElement ).HellowordOfTextblock. It will serve as ourView.

5.3. SetViewInjectRegionMedium

This work is doneModuleIn the module initialization. First, we need to find the specifiedViewTo be injectedRegion, AndRegionmanagerProvides the following functions:

Iregion mainregion = regionmanager. Regions ["mainregion"];

Then we canViewAddRegionAnd activated it:

VaR view = new helloworldview ();

Mainregion. Add (View );

Mainregion. Activate (View );

In general, ourHelloworldmoduleCodeAs follows:

Public class helloworldmodule: imodule

{

Private readonly iregionmanager regionmanager;

Public helloworldmodule (iregionmanager regionmanager)

{

This. regionmanager = regionmanager;

}

# Region imodule members

Public void initialize ()

{

Iregion mainregion = regionmanager. Regions ["mainregion"];

VaR view = new helloworldview ();

Mainregion. Add (View );

Mainregion. Activate (View );

}

# Endregion

}

OK . At this point, demo after creation, of course, step by step is unknown, but the introduction of Cal do not follow MVP or MVC .

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.