Silverlight按需載入–MEF方式–這篇文章簡單,跟著做一遍再說–以後會翻譯成中文

來源:互聯網
上載者:User

MEF gives us the ability to do this.  This post will cover the basics needed to build such a composite application split between different silerlight applications and download the referenced silverlight application only when needed.

Steps:

  1. Create a Silverlight 4 application
  2. Add references to the following assemblies:

System.ComponentModel.Composition.dll

System.ComponentModel.Composition.Initialization.dll

  1. Add a new Silverlight 4 application called ExternalSilverlightApplication to the solution that was created in step 1.  Ensure the new application is hosted in the web application for the solution and choose to not create a test page for the new application.
  2. Delete the App.xaml and MainPage.xaml files – they aren’t needed.
  3. Add references to the following assemblies in the ExternalSilverlightApplication project:

System.ComponentModel.Composition.dll

System.ComponentModel.Composition.Initialization.dll

  1. Ensure the two references above have their Copy Local values set to false.  As we will have these two assmblies in the original Silverlight application, we will have no need to include them in the built ExternalSilverlightApplication build.
  2. Add a new user control called LeftControl to the ExternalSilverlightApplication project.
  3. Replace the LayoutRoot Grid with the following xaml:

    <Grid x:Name="LayoutRoot" Background="Beige" Margin="40" >

        <Button Content="Left Content" Margin="30"></Button>

    </Grid>

  1. Add the following statement to the top of the LeftControl.xaml.cs file

using System.ComponentModel.Composition;

  1. Add the following attribute to the LeftControl class

    [Export(typeof(LeftControl))]

 

This attribute tells MEF that the type LeftControl will be exported – i.e. made available for other applications to import and compose into the application.

  1. Add a new user control called RightControl to the ExternalSilverlightApplication project.
  2. Replace the LayoutRoot Grid with the following xaml:

    <Grid x:Name="LayoutRoot" Background="Green" Margin="40"  >

        <TextBlock Margin="40" Foreground="White" Text="Right Control" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>

    </Grid>

  1. Add the following statement to the top of the RightControl.xaml.cs file

using System.ComponentModel.Composition;

  1. Add the following attribute to the RightControl class

    [Export(typeof(RightControl))]

  1. In your original Silverlight project add a reference to the ExternalSilverlightApplication project.
  2. Change the reference to the ExternalSilverlightApplication project to have it’s Copy Local value = false.  This will ensure that the referenced ExternalSilverlightApplication Silverlight application is not included in the original Silverlight application package when it it built.  The ExternalSilverlightApplication Silverlight application therefore has to be downloaded on demand by the original Silverlight application for it’s controls to be used.
  3. In your original Silverlight project add the following xaml to the LayoutRoot Grid in MainPage.xaml:

        <Grid.RowDefinitions>

            <RowDefinition Height="65*" />

            <RowDefinition Height="235*" />

        </Grid.RowDefinitions>

        <Button Name="LoaderButton" Content="Download External Controls" Click="Button_Click"></Button>

        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" >

            <Border Name="LeftContent" Background="Red" BorderBrush="Gray" CornerRadius="20"></Border>

            <Border Name="RightContent" Background="Red" BorderBrush="Gray" CornerRadius="20"></Border>

        </StackPanel>

 

 

 

The borders will hold the controls that will be downlaoded, imported and composed via MEF when the button is clicked.

  1. Add the following statement to the top of the MainPage.xaml.cs file

using System.ComponentModel.Composition;

  1. Add the following properties to the MainPage class:

        [Import(typeof(LeftControl))]

        public LeftControl LeftUserControl { get; set; }

        [Import(typeof(RightControl))]

        public RightControl RightUserControl { get; set; }

 

This defines properties accepting LeftControl and RightControl types.  The attrributes are used to tell MEF the discovered type that should be applied to the property when composition occurs.

  1. Add the following event handler for the button click to the MainPage.xaml.cs file:

        private void Button_Click(object sender, RoutedEventArgs e)

        {

 

                DeploymentCatalog deploymentCatalog =

    new DeploymentCatalog("ExternalSilverlightApplication.xap");

 

                CompositionHost.Initialize(deploymentCatalog);

 

                deploymentCatalog.DownloadCompleted += (s, i) =>

                {

                    if (i.Error == null)

                    {

                        CompositionInitializer.SatisfyImports(this);

 

                        LeftContent.Child = LeftUserControl;

                        RightContent.Child = RightUserControl;

                        LoaderButton.IsEnabled = false;

                    }

                };

 

                deploymentCatalog.DownloadAsync();

        }

This is where the magic happens!  The deploymentCatalog object is pointed to the ExternalSilverlightApplication.xap file.  It is then associated with the CompositionHost initialization.  As the download will be asynchronous, an eventhandler is created for the DownloadCompleted event.  The deploymentCatalog object is then told to start the asynchronous download.

The event handler that executes when the download is completed uses the CompositionInitializer.SatisfyImports() function to tell MEF to satisfy the Imports for the current class.  It is at this point that the LeftUserControl and RightUserControl properties are initialized with composed objects from the downloaded ExternalSilverlightApplication.xap package.

  1. Run the application click the Download External Controls button and see the controls defined in the ExternalSilverlightApplication application loaded into the original Silverlight application.

 

 

 

Congratulations!  You have implemented download on demand capabilities for composite applications using the MEF DeploymentCatalog class.  You are now able to segment your applications into separate xap file for deployment.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.