Prism Study Notes (1) starting from Hello World

Source: Internet
Author: User

Just like getting started, start with the great Hello world. In fact, there are already many entry-level legends of Prism on the Internet, but it's just a simple introduction to the operation process. Why do we need to write this?CodeAnd the principle behind it is rarely written. After reading it, I have some idea about it, but I don't know why.

Don't underestimate the simple "Hello World"ProgramIn fact, he has already included many important concepts, such as containers, dependency injection, why the bootstrapper class, and what is shell used, I will introduce them in detail in my notes.

Create a new Silverlight application named myprism.

After the creation, you can see an app. XAML and mainpage. XAML file. The role of APP. XAML is equivalent to the Global. asax file in the. NET web program, which includes the application_startup method.

 
Private VoidApplication_startup (ObjectSender, startupeventargs E)
{
This. Rootvisual =NewMainpage ();
}

This method implements the entire program entry, Application.The property of rootvisual is to get or set the main application user interface, which can only be set from the codeRootvisualAttribute Value, although the value can be obtained by any number of times. Mainpage. XAML is used as the initial Page.

We need to modify this part and replace it with our own bootstrapper.

 
Private VoidApplication_startup (ObjectSender, startupeventargs E)
{
Bootstrapper =NewBootstrapper ();
Bootstrapper. Run ();

}

The program will prompt that the bootstrapper class cannot be found, and we will create it later.

First, add assemblies of Prism:

    • Microsoft. Practices. Prism. dll
    • Microsoft. Practices. Prism. unityextensions. dll
    • Microsoft. Practices. servicelocation. dll
    • Microsoft. Practices. Unity. Silverlight. dll

Remember to go to The prism4 folder after installing prism and double-click to execute registerprismbinaries. BAT file, you can add all prism assemblies to the local class library, you can add reference in.. Net tab. For every specific purpose, I will be lazy here to post one official article.

Microsoft. Practices. Prism. dll. This Assembly contains the implementation of the prism library core components, such as modularity, logging services, communication services, and definitions for several core interfaces. it also contains the implementation of the prism library components that target Silverlight applications, including commands, regions, and events.

Microsoft. Practices. Prism. unityextensions. dll. This Assembly contains base and Utility Classes you can reuse in applications built with the prism library that consume the unity Application Block. For example, it contains a bootstrapper base class,UnitybootstrapperClass, that creates and configures a unity container with default prism library services when the application starts.

Microsoft. Practices. Unity. Silverlight. dll. This Assembly enables you want to use the unity Application Block in your application. by default, applications built using prism use the unity Application Block. however, developers who prefer to use different container implementations can build adapters for them using the provided extensibility points in the prism library.

Microsoft. Practices. servicelocation. dll. This Assembly contains the common service locator interface used by Prism to provide an alternative action over inversion of control (IOC) containers and service locators; therefore, you can change the container implementation with counter.

Before creating a bootstrapper, rename mainpage. XAML to shell. XAML. The method is as follows:

Role of shell. XAML files:

The idea of prism is to use the shell page to display the page, divide it into different region, and embed other written modules into the corresponding region in the shell page, and finally present it to the user.

Modify the content of the shell. XAML file

<  Usercontrol  X: Class  = "Myprism. Shell"  
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns: Regions = "Http://www.codeplex.com/prism"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400" >

< Itemscontrol Name = "Mainregion" Regions: regionmanager. regionname = "Mainregion" />

</ Usercontrol >

 

The key is this sentence. <ItemscontrolName= "Mainregion"Regions: regionmanager. regionname= "Mainregion"/>The shell contains a region named "mainregion.

Xmlns: Regions= "Http://www.codeplex.com/prism"
Used to reference prism assembly.

Next, create a module that displays the page content, add new project, select Silverlight class library, and name it helloworldmodule.

Create a folder named views and add a Silverlight user control file named helloworldview. XAML to the folder.

Helloworldview. XAML contains the final content to be displayed. Add a textblock control to display the hello World content.

 <  Usercontrol  X: Class  = "Helloworldmodule. Views. helloworldview"  
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400" >

< Grid X: Name = "Layoutroot" Background = "White" >
< Textblock Text = "Hello World" Foreground = "Green" Horizontalalignment = "Center" Verticalalignment = "Center" Fontfamily = "Calibri" Fontsize = "24" Fontweight = "Bold" > </ Textblock >
</ Grid >
</ Usercontrol >

Next, rename class. CS to helloworldmodule. CS.

Do you still remember the shell. XAML file we created earlier? Helloworldmodule. CS will bind helloworldview. XAML to mainregion in shell. The following is the helloworldmodule. CS code. Because Microsoft. Practices. Prism. modularity and Microsoft. Practices. Prism. Regions are referenced here, you need to add Microsoft. Practices. Prism to the reference.

 Using Microsoft. Practices. Prism. modularity;
Using Microsoft. Practices. Prism. regions;

Namespace Helloworldmodule
{
Public Class Helloworldmodule: imodule
{
Private Readonly Iregionmanager regionmanager;

Public Helloworldmodule (iregionmanager regionmanager)
{
This . Regionmanager = regionmanager;
}

Public Void Initialize ()
{
Regionmanager. registerviewwithregion ( " Mainregion " , Typeof (Views. helloworldview ));
}
}
}

Here, the concept of dependency injection is used. Dependency injection is also called reverse injection IOC. "This makes the object only dependent on the IOC container and does not depend on each other. Instead, when the object is created, the IOC container injects the dependent object into the inject body, so it is also called the dependency injection mode. "For the first time, I can see this definition is also inexplicable, later, I found that dependency injection is very simple.ArticleA lot, here there is a link to the http://www.cnblogs.com/xingyukun/archive/2007/10/20/931331.html to talk about pretty good, interested can look.

The helloworldmodule class inherits the imodule interface. Only in this way can it be added to the module collection of the container.

In the helloworldmodule class, when defining the constructor of the class, the iregionmanager regionmanager object is injected. That is to say, the helloworldmodule does not create its own iregionmanager object, and it will accept the iregionmanager object from the container. I will mention this in the last introduction to bootstrapper.

Remember what we added in shell. XAML<ItemscontrolName= "Mainregion"Regions: regionmanager. regionname= "Mainregion"/>Controls?,Initialize () registers helloworldview to mainregion in regionmanager.

How is this implemented? Let's go to the last step to create the bootstrapper class. Return to myprism and create a new bootstrapper. CS file. Modify the Code as follows:

 Using System;
Using System. windows;
Using Microsoft. Practices. Prism. modularity;
Using Microsoft. Practices. Prism. unityextensions;
Using Microsoft. Practices. Unity;

Namespace Myprism
{
Public Class Bootstrapper: unitybootstrapper
{
Protected Override Dependencyobject createshell ()
{
Return Container. Resolve <shell> ();
}

Protected Override Void Initializeshell ()
{
Base . Initializeshell ();
Application. Current. rootvisual = (uielement) This . Shell;
}

Protected Override Void Configuremodulecatalog ()
{
Base . Configuremodulecatalog ();

Modulecatalog modumodu= (modulecatalog) This . Modulecatalog;
Modulecatalog. addmodule ( Typeof (Helloworldmodule. helloworldmodule ));
}


}
}

The bootstrapper class integrates the previously created shell and helloworldmodule.

There are three methods: configuremodulecatalog, createshell, and initializeshell:

First, add the helloworldmodule to modulecatalog in configuremodulecatalog (). This modulecatalog will manage all modules.

Then createshell () generates a shell instance which will serve as the homepage.

At last, initializeshell () assigns the shell instance to application. Current. rootvisual, which is displayed on the page. (At the beginning of this article, we modified the app. XAML. CS file and moved the assignment of application. Current. rootvisual here to complete the assignment ).

Beginners may not be able to understand container. Resolve <shell> (); usage, here there is an article written very clearly, you can look at the http://www.cnblogs.com/inrie/archive/2008/04/18/1159002.html

In fact, container. resolve <shell> (); returns a shell instance, which is controlled by the container. Generally, you must use the container before using resolve to obtain the instance. registertype is used to register a class. If the class is not registered, an instance is returned. You can also use new:

 
Protected OverrideDependencyobject createshell ()
{
Return NewShell ();
}

Our shell is relatively simple, just a control. The advantage of using container is that, however, if you encounter a complicated shell, its reference depends on other classes, resolve can automatically help you create other classes and return them to an instance. You don't have to worry about the relationships between those classes in the background, you can fix all these troubles.

Here we are all done. Press F5 to obtain the following result:

 

This is the most basic application of Prism, which includes its main idea. The mvvm mode will be discussed later.

Note: My writing style may be difficult for readers. Most formal tutorials generally write the principle framework first, and then write the actual application instances, instance configurations, and principles. However, I do not like this very much. I often see the principles in the beginning in the clouds, and I am very easy to understand. When you see the following actual application, you do not know which principle to combine with the previous one, and the efficiency is very low. I think it is better to combine theory with practice to distribute the theoretical content to specific applications. First, practice and analyze the principles when encountering obstacles. This is a sense of accomplishment, and it is not easy to be intimidated by a bunch of concepts. I don't know how it works...

Source code: Http://files.cnblogs.com/mindflying/MyPrism.zip

 

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.