背景

由于公司又要开发一款新的设备,新的设备是基于老设备进行改造,新增加了一部分功能,因此就在老版本代码上延伸出来一个分支进行改造,当时为了赶进度,所以代码也是走马观花看了一遍,最终还是经过不懈的努力,将这个项目给做了出来。现在觉得抽空可以好好研究一下这一块的代码,首当其冲的是MEF框架。

MEF框架介绍

刚开始看这块代码的时候,本人也是不太了解这一块代码的作用,因此也存在了很多困惑

困惑一:对程序员而言,自定义对象必须构建对象后才能使用,但是代码中没有构建实例即可使用

困惑二:代码当中使用了很多特性,例如InheritedExport,Export等诸多特性

带着这些困惑,我打算去揭开这段代码的神秘面纱,通过网上相关资料查阅,才知道这是引入了MEF框架,官方文档给的解释是:托管可扩展框架,对于这些概念性的东西还是很含糊,所以决定自己动手做一个案例,通过案例去分析这段代码的作用

案例:

MEF框架使用 - 图1

这是项目主体框架,下面我会介绍各个类库之间的关系

Flex.MEF.Interface

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Flex.MEF.Interface
  8. {
  9. [InheritedExport]
  10. public interface IView
  11. {
  12. }
  13. }

Flex.MEF.Implete

  1. <UserControl x:Class="Flex.MEF.Implete.Chinese.ChineseBook"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <Label Content="Hello,我是语文书" FontSize="20" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  10. </Grid>
  11. </UserControl>
  12. using Flex.MEF.Interface;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel.Composition;
  16. using System.Linq;
  17. using System.Reflection;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Documents;
  24. using System.Windows.Input;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Imaging;
  27. using System.Windows.Navigation;
  28. using System.Windows.Shapes;
  29. namespace Flex.MEF.Implete.Chinese
  30. {
  31. /// <summary>
  32. /// ChineseBook.xaml 的交互逻辑
  33. /// </summary>
  34. [Export(typeof(IView))]
  35. [CustomModuleMetadata(ModuleName.Chinese)]
  36. public partial class ChineseBook : UserControl, IView
  37. {
  38. public ChineseBook()
  39. {
  40. InitializeComponent();
  41. }
  42. }
  43. }
  1. <UserControl x:Class="Flex.MEF.Implete.English.EnglishBook"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <Label Content="Hello,我是英语书" FontSize="20" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  10. </Grid>
  11. </UserControl>
  12. using Flex.MEF.Interface;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel.Composition;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Data;
  22. using System.Windows.Documents;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using System.Windows.Navigation;
  27. using System.Windows.Shapes;
  28. namespace Flex.MEF.Implete.English
  29. {
  30. /// <summary>
  31. /// EnglishBook.xaml 的交互逻辑
  32. /// </summary>
  33. [Export(typeof(IView))]
  34. [CustomModuleMetadata(ModuleName.English)]
  35. public partial class EnglishBook : UserControl, IView
  36. {
  37. public EnglishBook()
  38. {
  39. InitializeComponent();
  40. }
  41. }
  42. }
  1. <UserControl x:Class="Flex.MEF.Implete.Math.MathBook"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="300" d:DesignWidth="300">
  8. <Grid>
  9. <Label Content="Hello,我是数学书" FontSize="20" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  10. </Grid>
  11. </UserControl>
  12. using Flex.MEF.Interface;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel.Composition;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Data;
  22. using System.Windows.Documents;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using System.Windows.Navigation;
  27. using System.Windows.Shapes;
  28. namespace Flex.MEF.Implete.Math
  29. {
  30. /// <summary>
  31. /// MathBoos.xaml 的交互逻辑
  32. /// </summary>
  33. [Export(typeof(IView))]
  34. [CustomModuleMetadata(ModuleName.Math)]
  35. public partial class MathBook : UserControl, IView
  36. {
  37. public MathBook()
  38. {
  39. InitializeComponent();
  40. }
  41. }
  42. }

启动项

  1. <Window x:Class="Flex.MEF.Test.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Grid x:Name="MyGrid">
  6. <Grid.RowDefinitions>
  7. <RowDefinition Height="80"></RowDefinition>
  8. <RowDefinition></RowDefinition>
  9. </Grid.RowDefinitions>
  10. <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
  11. <Button Content="语文书" x:Name="ChineseBook" Width="120" Height="40" Margin="10,0" Click="ChineseBook_Click"></Button>
  12. <Button Content="数学书" x:Name="MathBook" Width="120" Height="40" Margin="10,0" Click="MathBook_Click"></Button>
  13. <Button Content="英语书" x:Name="EnglishBook" Width="120" Height="40" Margin="10,0" Click="EnglishBook_Click"></Button>
  14. </StackPanel>
  15. <Grid x:Name="WorkPanel" Grid.Row="1">
  16. </Grid>
  17. </Grid>
  18. </Window>
  19. using Flex.MEF.Interface;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.ComponentModel.Composition;
  23. using System.ComponentModel.Composition.Hosting;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Threading.Tasks;
  28. using System.Windows;
  29. using System.Windows.Controls;
  30. using System.Windows.Data;
  31. using System.Windows.Documents;
  32. using System.Windows.Input;
  33. using System.Windows.Media;
  34. using System.Windows.Media.Imaging;
  35. using System.Windows.Navigation;
  36. using System.Windows.Shapes;
  37. namespace Flex.MEF.Test
  38. {
  39. /// <summary>
  40. /// MainWindow.xaml 的交互逻辑
  41. /// </summary>
  42. public partial class MainWindow : Window
  43. {
  44. [ImportMany(typeof(IView))]
  45. public Lazy<IView, IMetadata>[] Services { get; set; }
  46. public UserControl MyUserControl { get; set; }
  47. public MainWindow()
  48. {
  49. InitializeComponent();
  50. Load();
  51. }
  52. private void LoadView(ModuleName moduleName)
  53. {
  54. if (Services != null)
  55. {
  56. this.WorkPanel.Children.Clear();
  57. foreach (var item in Services)
  58. {
  59. if (item.Metadata.ModuleName == moduleName)
  60. {
  61. MyUserControl = (UserControl)item.Value;
  62. break;
  63. }
  64. }
  65. this.WorkPanel.Children.Add(MyUserControl);
  66. }
  67. }
  68. private void Load()
  69. {
  70. var dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
  71. if (dir.Exists)
  72. {
  73. var catalog = new AggregateCatalog();
  74. catalog.Catalogs.Add(new DirectoryCatalog(dir.FullName));
  75. var container = new CompositionContainer(catalog);
  76. try
  77. {
  78. container.ComposeParts(this);
  79. }
  80. catch (CompositionException ex)
  81. {
  82. throw new Exception(ex.Message);
  83. }
  84. }
  85. }
  86. private void ChineseBook_Click(object sender, RoutedEventArgs e)
  87. {
  88. LoadView(ModuleName.Chinese);
  89. }
  90. private void MathBook_Click(object sender, RoutedEventArgs e)
  91. {
  92. LoadView(ModuleName.Math);
  93. }
  94. private void EnglishBook_Click(object sender, RoutedEventArgs e)
  95. {
  96. LoadView(ModuleName.English);
  97. }
  98. }
  99. }

总结

通过上面例子,我们可以看到ChineseBook,EnglishBook,MathBook三者都是实现了IView接口,并且通过添加特性标签Export将这个页面导出为IView类型,然后启动项使用特性标签ImportMany导出来一个列表,最核心的部分为Load方法,它会去启动项Debug目录加载dll,这样我们就可以将导出类型为IView类型的页面注入到标记为ImportMany的属性中,其中我添加了一个ModuleName的特性,主要是用来区分页面

效果图

MEF框架使用 - 图2

点击页面上的按钮,就可以进行页面进行切换