[Silverlight] use Prism to build your own program development architecture

Source: Internet
Author: User

We all know the example of stocktraderri provided by Prism. This example shows how to combine different functional modules (including pages) into the main interface (Shell; I was surprised to see this example at the beginning because I have never experienced such a requirement in my project experience, but the most frequently encountered is "Display (CALL) on the main interface) pages in different modules ": for example, I have a menu on the main interface,

1. When I click a menu item, I want to display a page in a module in the content area under the menu of the main interface, and the displayed animation effect is required,

This animation is not very effective. In fact, the page is switched by sliding to the left. You can download it.CodeYou can see it,
2. Each module must be loaded as needed, and the progress of the module loading process must be displayed,

3. All adopt mvvm design.
I think there may also be other people who have had such confusions. So in order to reduce detours and wheel creation, we have this essay, which makes us no longer confused with the example of stocktraderri, more suitable for your projectProgramDevelopment Framework.

My project structure

Prismdemomain is the main Silverlight program, and prismdemomain. Web is used to host the main program;
Primdemocommon is the Silverlight class library, which is provided to the main program and various module projects for reference (providing public objects ).
The two projects modulea and moduleb under the modules solution folder correspond to the modulea and moduleb modules respectively. For example, the object code of modulea is as follows:

Using System;
Using Microsoft. Practices. Prism. modularity;
Using Microsoft. Practices. Prism. mefextensions. modularity;
Using System. componentmodel. composition;
Using Prismdemocommon;

namespace modulea
{< br> [moduleexport ( typeof (modulea)]
Public class modulea: imodule
{

Public VoidInitialize ()
{
//Throw new notimplementedexception ();
}
}
}

By the way, I personally prefer MEF. In prism, I only use the MEF extension, such as mefbootstrapper, and even hate unity. It may be the reason for code cleansing, in fact, the entire program can use MEF to manage object initialization and eliminate the new object code. You can refer to the Code in my previous article.

The modulea project contains two folders: Views and viewmodels. You all know about the two folders. It is worth mentioning that I divide each view in views into two categories, one being mainview, inherited from the imainview interface. The other class does not need to inherit this interface. mainview is used to display the view in the content area of the main interface. inherited from imainview is also used to facilitate display in the content area of the main interface, other views are called by mainview.

The view1 code in modulea is as follows:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using System. componentmodel. composition;
Using Modulea. viewmodels;
Using Prismdemocommon;

Namespace Modulea. Views
{
[Export ( " Modulea $ view1 " , Typeof (Imainview)]
[Partcreationpolicy (creationpolicy. Shared)]
Public   Partial   Class View1: usercontrol, imainview
{
[Importingconstructor]
Public View1 (viewmodel1 viewmodel)
{
Initializecomponent ();
This . Datacontext = Viewmodel;
}


}
}

Take a look at the module modulescatalog. XAML file, which is very simple.

< Modularity: modulecatalog Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"  
Xmlns: SYS = "CLR-namespace: system; Assembly = mscorlib"  
Xmlns: modularity = "CLR-namespace: Microsoft. Practices. Prism. modularity; Assembly = Microsoft. Practices. Prism" >
<! -- Module info without a group -->
< Modularity: moduleinfo Ref = "Modulea. xap" Modulename = "Modulea" Moduletype = "Modulea. modulea, modulea" Initializationmode = "OnDemand"   />
< Modularity: moduleinfo Ref = "Moduleb. xap" Modulename = "Moduleb" Moduletype = "Moduleb. moduleb, moduleb" Initializationmode = "OnDemand"   />
</ Modularity: modulecatalog >

There is a mainpagemodule folder in the prismdemomain main program. In fact, it is also a prism module, which is the real main interface implementation of the program. The view is injected into the shell, with a line of code in the shell:

< Contentcontrol Regions: regionmanager. regionname = "Mainpageregion" Horizontalcontentalignment = "Stretch" Verticalcontentalignment = "Stretch"   />

The advantage of doing so is that if I am not satisfied with this main interface, I can easily replace it with other mainpagemodules, or even make multiple main interfaces for users to choose from.
The loading module, display page, and animation effect are all completed in this module. For example, you can click the menu to display the code for switching the View:

  Private   Void Showcurrentmainview ()
{
// Obtains the current master view instance.
Imainview mainview = Servicelocator. Current. getinstance < Imainview > (Currentmainviewname );
If (Mainview ! =   Null )
{
Eventaggregator. getevent < Showmainviewevent > (). Publish (mainview );
}
}

Private   Void Activeview ( Object OBJ)
{
String Mainviewname = OBJ. tostring ();
// If the current view is the view to be activated, return
If (Currentmainviewname = Mainviewname) Return ;
Currentmainviewname = Mainviewname;
// Obtain the name of the module required to activate the view.
String Modulename = Currentmainviewname. substring ( 0 , Currentmainviewname. indexof ( " $ " ));
// Initialize modulemanager and load events.
Initializemodulemanager ();
// Obtain module information based on the module name
Modulecatalog modumoducat = Servicelocator. Current. getinstance < Modulecatalog > ();
If (Modulecatalog =   Null ) Return ;
Moduleinfo = Modulecatalog. modules. firstordefault (P => P. modulename = Modulename );
If (Moduleinfo =   Null ) Return ;
// Determine whether the module has been initialized
If (Moduleinfo. State = Modulestate. initialized)
{
// Show current main view
Showcurrentmainview ();
}
Else   If (Modulemanager ! =   Null )
{
Modulemanager. loadmodule (modulename );
}
}

Events that display the main view are triggered, and the main view is animated. For more information, seeSource code.

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.