MEF架構在Silverlight中應用(15)

來源:互聯網
上載者:User

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外掛程式機制可以使程式靈活擴充。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.