我学MEF系列(6):在Silverlight中应用MEF

前言

  通过前面MEF的基础知识的学习,我们已经知道MEF可以在控制台程序中寄宿。MEF还可以在Winform,Silverlight,WPF等应用程序中使用。在接下来的几篇文章中我们就重点介绍MEF是如何在这些应用程序中使用的。本文通过一个例子介绍MEF在Silverlight中的简单应用。

正文

第一步:打开VS2010新建一个Silverlight应用程序,命名为"MEFDemo_SL"。一路默认最后Solution的结构如下:

在MainPage的设计器中添加一个容器控件,我们在这里添加一个TabControl(Name设置为"tcContainer")。

xaml的代码如下:

<UserControl x:Class="MEFDemo_SL.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<sdk:Label Grid.Row="0" Margin="10" FontSize="24" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Content="在Silverlight中使用MEF示例" />
<sdk:TabControl Grid.Row="1" HorizontalAlignment="Left" Name="tcContainer" VerticalAlignment="Top" Width="500" Height="300">
<sdk:TabItem Header="MyTab1" Name="tiTabItem1">
</sdk:TabItem>
<sdk:TabItem Header="MyTab2" Name="tiTabItem2">
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Height="27" Width="100" Content="转到MyTab1"></Button>
</sdk:TabItem>
</sdk:TabControl>
</Grid>
</UserControl>

第二步:添加一个Silverlight类库,命名为"MEFDemo_SL.Contract"。删除默认的class1,新建接口IMyPart。

1 namespace MEFDemo_SL.Contract
2 {
3 public inerface IMyPart
4 {
5
6 }
7 }

第三步:创建扩展控件。主要步骤为:

1.添加一个Silverlight类库,命名为“MEFDemo_SL.Extensions”;

2.删除默认类。添加对程序集“System.ComponentModel.Composition.dll”和项目MEFDemo_SL.Contract的引用,并在类上标记导出特性[Export(typeof(IMyPart))];

3.新建用户控件命名为“MyExtension1”,使之实现接口IMypart。并添加一个富文本控件“RichTextBox” 命名为“rtbTest”.

xaml代码:

<UserControl x:Class="MEFDemo_SL.Extensions.MyExtension1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">

<Grid x:Name="LayoutRoot" Background="White">
<RichTextBox HorizontalAlignment="Left" Margin="10" Name="rtbTest" VerticalAlignment="Top" Height="400" Width="600" BorderThickness="2" Background="Bisque" FontSize="20"/>
</Grid>
</UserControl>

cs文件代码:

 1 using System.Windows.Controls;
2 using MEFDemo_SL.Contract;
3 using System.ComponentModel.Composition;
4
5 namespace MEFDemo_SL.Extensions
6 {
7 [Export(typeof(IMyPart))]
8 public partial class MyExtension1 : UserControl,IMyPart
9 {
10 public MyExtension1()
11 {
12 InitializeComponent();
13 }
14 }
15 }

第四步:操作MEFDemo_SL项目,实现在MainPage中加载扩展部件MyExtension1.

1.在项目MEFDemo_SL中添加对MEFDemo_SL.Contract的项目引用;

2.添加对程序集“System.ComponentModel.Composition.dll”和“System.ComponentModel.Composition.Initialization.dll”的引用

3.定义ContractType为IMyPart的导入:

1  [ImportMany]
2 public IEnumerable<IMyPart> Parts { get; set; }

4.组合,为了使扩展控件能够被加载我们添加对项目“MEFDemo_SL.Estensions”的引用(下一篇会介绍如何动态加载xap包)。我们在MainPage的cs文件代码如下:

 1 using System.Collections.Generic;
2 using System.ComponentModel.Composition;
3 using System.ComponentModel.Composition.Hosting;
4 using System.Windows.Controls;
5 using MEFDemo_SL.Contract;
6 using MEFDemo_SL.Extensions;
7
8 namespace MEFDemo_SL
9 {
10 public partial class MainPage : UserControl
11 {
12 public MainPage()
13 {
14 InitializeComponent();
15
16 //var catalog = new AggregateCatalog(new DeploymentCatalog());
17        //var container = new CompositionContainer(catalog);
18        //container.ComposeParts(this);
19
20 CompositionInitializer.SatisfyImports(this); //使用上面的三行代码也可完成组合
21
22 foreach (var part in Parts)
23 {
24 tiTabItem1.Content = part;
25 }
26 }
27
28 [ImportMany(typeof(IMyPart))]
29 public IEnumerable<IMyPart> Parts { get; set; }
30 }
31 }


 

通过上面几步的操作我们我们可以看到界面效果如下:

到此我们已经完成了在Silverlight中使用MEF。

示例源码下载:MEFDemo_SL.rar


 

作者:ps_zw
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/pszw/p/2293972.html