MEF架構在Silverlight中應用
代碼下載
MEF架構是微軟提供的一個外掛程式架構。應用概括為一句話:輸入,輸出,組合。下面是具體在Silverlight中應用中的關鍵代碼。
步驟
1.Client端建立3個Silverlight項目。
MEFSilverLight4(主專案,組裝組件)
SilverlightApplication1(組件)
SilverlightApplication2(組件)
2.Server建立一個測試專案。調用Silverlight
3.添加引用類庫組件
1)MEFSilverLight4項目中添加引用
System.ComponentModel.Composition.dll,System.ComponentModel.Composition.Initialization.dll
關鍵代碼:
//定義輸出
[Export(typeof(UserControl))]
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}
組合組件關鍵代碼:
//匯入組件
[Import(typeof(UserControl))]
public UserControl myUI { get; set; }
//組裝組件:1.載入程式包。2.將資料包初始化到宿主中。3.非同步下載程式包。4.下載成功後組合組件,應用組件。
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);//載入程式包
CompositionHost.Initialize(de);//初始化到宿主中
//非同步下載
de.DownloadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(de_DownloadCompleted);
de.DownloadAsync();
}
/// <summary>
/// 非同步下載完成
/// </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);//組合組件
this.RootVisual = myUI;//顯示
}
}
2)SilverlightApplication1,添加引用System.ComponentModel.Composition.dll
關鍵代碼:
//定義輸出
[Export(typeof(UserControl))]
public partial class SLU1 : UserControl
{
public SLU1()
{
InitializeComponent();
}
}
3)SilverlightApplication2,添加引用System.ComponentModel.Composition.dll
關鍵代碼:
//定義輸出
[Export(typeof(UserControl))]
public partial class SLU2 : UserControl
{
public SLU2()
{
InitializeComponent();
}
}
4,Web調用代碼
1)載入主程式包,建立項目時自動產生。
<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">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="擷取 Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
2)測試代碼
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/MEFSilverLight4TestPage.aspx?UI=1" Target="_blank">測試1</asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/MEFSilverLight4TestPage.aspx?UI=2" Target="_blank">測試2</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/MEFSilverLight4TestPage.aspx?UI=3" Target="_blank">測試3</asp:HyperLink>
4.總結
1)可以解決將Silverlight項目解耦,分離項目之間的依賴關係。
2)避免.xap包過大,網路下載比較慢,可以實現按需載入。
3)mef外掛程式機制可以使程式靈活擴充。