Prism Research (for WPF & Silverlight) 4. Starting from Hello World)

Source: Internet
Author: User

This article guides you through the Prism framework to create a WPF version of the Hello World Program. The program of the Silverlight version is similar and will not be described.

See the documentation P127-147 for sample code in the Quickstarts \ Hello World Directory.

1. First, we need to prepare six dll of the Prism framework, which are:

· Microsoft. Practices. Composite. dll

· Microsoft. Practices. Composite. Presentation. dll

· Microsoft. Practices. Composite. UnityExtensions. dll

· Microsoft. Practices. ObjectBuilder2.dll

· Microsoft. Practices. ServiceLocation. dll

· Microsoft. Practices. Unity. dll

You can find these dll directly from the LIB directory, compile projects in the CAL directory, and find them in the bin \ debug directory. Copy them to a directory so that they can be conveniently used in multiple projects. At the same time, you will see a lot of xml files whose names are the same as those of these dll-it is best to copy them together, these xml files are used for intelligent perception in Visual Studio.

2. Create a Shell

Create a WPF application named HelloWorld. Desktop. Add references to all the above dll files. Then, delete the default Window1.xaml and create a new Shell. xaml.

3. Add Region

Add a reference to the Prism namespace in Shell. xaml as follows:

xmlns:cal="http://www.codeplex.com/CompositeWPF"

In this way, we can add the dependency attribute in Prism for Region.

Delete the Grid tag, replace it with ItemsControl, and name it MainRegion:

<ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" />

Here cal: RegionManager. RegionName is a dependency attribute, which is associated with the ItemsControl control.

The modified Shell. xaml is as follows:

<Window x:Class="HelloWorld.Desktop.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">    <ItemsControl Name="MainRegion" cal:RegionManager.RegionName="MainRegion" /></Window>

4. Add Bootstarpper

In the HelloWorld. Desktop project, add a class named Bootstarpper. Here the CreateShell method is overwritten to create a Shell form object and display:

class Bootstrapper : UnityBootstrapper{    protected override DependencyObject CreateShell()    {        Shell shell = new Shell();        shell.Show();        return shell;    }}

* Note: you must add a reference to a namespace to use UnityBootstrapper:

using Microsoft.Practices.Composite.UnityExtensions;

5. Modify the App. xaml file.

In the App. xaml file, delete StartupUri = "Window1.xaml" in the Application tag ". Override the OnStartup method in the corresponding background file App. xaml. cs:

protected override void OnStartup(StartupEventArgs e){    base.OnStartup(e);    Bootstrapper bootstrapper = new Bootstrapper();    bootstrapper.Run();}

6. Create a class library named HelloWorldModule and add references to two of the Prism class libraries:

· Microsoft. Practices. Composite. dll

· Microsoft. Practices. Composite. Presentation. dll

Delete the automatically generated class1.cs file.

7. Create a folder in the HelloWorldModule class library named Views, and create HelloWorldView. xaml in this folder. We will add a TextBlock that displays HelloWorld for this xaml:

<UserControl x:Class="HelloWorldModule.Views.HelloWorldView"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    >    <Grid>        <TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>    </Grid></UserControl>

8. Add a class named HelloWorldModule to the HelloWorldModule class library, which is derived from the IModule interface. Therefore, we need to implement its Initialize method:

public class HelloWorldModule : IModule{    private readonly IRegionViewRegistry regionViewRegistry;    public HelloWorldModule(IRegionViewRegistry registry)    {        this.regionViewRegistry = registry;       }    public void Initialize()    {        regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));    }}

We found that in the Initialize method, the MainRegion specified in step 1 is associated with the HelloWorldView class (this is a xaml) in the folder Views.

At the same time, we use the dependency injection (constructor) method to pass the regionViewRegistry parameter.

9. Return to the HelloWorld. Desktop application and add a reference to the HelloWorldModule class library.

10. Finally, return to the Bootstarpper created in step 1, and overwrite its GetModuleCatalog method to find and load the specified module:

protected override IModuleCatalog GetModuleCatalog(){    ModuleCatalog catalog = new ModuleCatalog()        .AddModule(typeof(HelloWorldModule.HelloWorldModule));    return catalog;}

 

* Note: you must add a reference to a namespace to use IModuleCatalog and ModuleCatalog:

using Microsoft.Practices.Composite.Modularity;
The code in this example is located in the HelloWorld directory in QuickStart.

Supplement:

Prism is based on Silverlight 2.0, but because it is only a framework, it is also applicable to the current Silverlight 3 beta1 version. Several Silverlight examples are provided in Prism, you need to convert the sln file to open it. Here, we only care about the framework, not the version.

The Hello World version of Silverlight is also located in the HelloWorld directory in QuickStart.

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.