MEF framework application in Silverlight (15)

Source: Internet
Author: User
Tags silverlight

Original: MEF framework applied in Silverlight (15)

The MEF framework is applied in Silverlight


Code download

The MEF framework is a plug-in framework provided by Microsoft. The application is summed up in a sentence: input, output, combination. The following are the key codes that are specifically applied in Silverlight.
Steps
Build 3 Silverlight projects on the 1.Client side.
MEFSILVERLIGHT4 (main project, assembled parts)
SilverlightApplication1 (Parts)
SilverlightApplication2 (Parts)
2.Server build a test project. Call Silverlight

3. Adding a reference Class library component
1) Add references to the MEFSILVERLIGHT4 project
System.componentmodel.composition.dll,system.componentmodel.composition.initialization.dll
Key code:
Defining the output
[Export (typeof (UserControl))]
public partial class Mainpage:usercontrol
{
Public MainPage ()
{
InitializeComponent ();
}
}


Assembly Key Code:

Importing parts
[Import (typeof (UserControl))]
Public UserControl Myui {get; set;}

Assembly parts: 1. Load the package. 2. Initialize the packet to the host. 3. Download the package asynchronously. 4. After the successful download, assemble the parts and apply the parts.
private void Application_Startup (object sender, StartupEventArgs e)
{
String typeui = htmlpage.document.querystring["UI"];
String strxap = "";
if (Typeui = = "1")
{
STRXAP = "Silverlightapplication1.xap";
}
else if (Typeui = = "2")
{
STRXAP = "Silverlightapplication2.xap";
}
Else
{
STRXAP = "Mefsilverlight4.xap";
}
Deploymentcatalog de = new Deploymentcatalog (STRXAP);//Load Package
Compositionhost.initialize (DE);//Initialize into host
Asynchronous download
De. downloadcompleted + = new eventhandler<system.componentmodel.asynccompletedeventargs> (de_DownloadCompleted);
De. Downloadasync ();
}
<summary>
Asynchronous Download complete
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void De_downloadcompleted (object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (E.error = = null)
{
Compositioninitializer.satisfyimports (this);//Combination Parts
This. RootVisual = myui;//Display
}
}

2) SilverlightApplication1, add Reference System.ComponentModel.Composition.dll
Key code:
Defining the output
[Export (typeof (UserControl))]
public partial class Slu1:usercontrol
{
Public SLU1 ()
{
InitializeComponent ();
}
}
3) SilverlightApplication2, add Reference System.ComponentModel.Composition.dll
Key code:
Defining the output
[Export (typeof (UserControl))]
public partial class Slu2:usercontrol
{
Public SLU2 ()
{
InitializeComponent ();
}
}
4,web Calling code
1) load the main package and generate it automatically when the project is created.
<object data= "data:application/x-silverlight-2," type= "Application/x-silverlight-2" width= "100%" height= "100%" >
<param name= "source" value= "clientbin/mefsilverlight4.xap"/>
<param name= "OnError" value= "Onsilverlighterror"/>
<param name= "Background" value= "white"/>
<param name= "minRuntimeVersion" value= "4.0.50826.0"/>
<param name= "AutoUpgrade" value= "true"/>
<a href= "http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style= "Text-decoration:none" >

</a>
</object>
2) test Code
<asp:hyperlink id= "HYPERLINK2" runat= "Server" Navigateurl= "~/mefsilverlight4testpage.aspx? Ui=1 "target=" _blank "> Test 1</asp:hyperlink>
<asp:hyperlink id= "HyperLink1" runat= "Server" Navigateurl= "~/mefsilverlight4testpage.aspx? ui=2 "target=" _blank "> Test 2</asp:hyperlink>
<asp:hyperlink id= "HyperLink3" runat= "Server" Navigateurl= "~/mefsilverlight4testpage.aspx? Ui=3 "target=" _blank "> Test 3</asp:hyperlink>

4. Summary
1) You can solve the decoupling of Silverlight projects, separating dependencies between projects.
2) Avoid the. xap package is too large, the network download is slow, you can implement on-demand loading.
3) The MEF plug-in mechanism allows the program to scale flexibly.

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.