Thousands of years later...
嘿!我回来了!
试了半天,我总算想起账号密码了!
作为补偿,我会半口气教你们怎么从零开始写MEF!
1.用你的Visual Studio开一个WPF(.NET Framework)项目
2.在这个项目中做一个TabControl,取名为Homepage
<TabControl x:Name="Homepage">
<TabItem>
<TabItem.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Source="AddInStore.png"/>
</Grid>
</TabItem.Header>
<ListBox SelectionChanged="MyAddIns_SelectionChanged" x:Name="MyAddIns" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="32"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border x:Name="MyIcon" Grid.Column="0">
</Border>
<StackPanel Grid.Column="1">
<TextBlock Text="{Binding Title}" FontWeight="Bold" Margin="3"/>
<TextBlock Text="{Binding Description}" FontWeight="Bold" Margin="3"/>
<TextBlock Text="{Binding Version}" FontWeight="Bold" Margin="3"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</TabItem>
</TabControl>
好了,我们要开始写C#了,我们先把插件中转器做一做吧。。。
添加一个类库,并命名为Contract
右键打开属性,改一下输出路径为..\Output
并把默认命名空间调整为一致:
(顺便也把主程序的输出路径改为..\Output!)
在此项目中,新加一个类(class)
命名为IMEFLauncherExtension.cs
这次要添加一个using System.Windows,默认的类库是没有这个using的。。。
代码还是如下
using System.Windows;
namespace MEFLauncher
{
public interface IMEFLauncherExtension
{
//插件的基本信息
string Title { get; }
string Description { get; }
//插件UI的获取
FrameworkElement GetUI();
}
}
回到MainWindow上面来,像这样做一个ListBox!用来打开插件
<ListBox SelectionChanged="MyAddIns_SelectionChanged" x:Name="MyAddIns" Grid.Column="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3,3,0,8" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Title}" FontWeight="Bold" Margin="3"/>
<TextBlock Text="{Binding Description}" FontWeight="Bold" Margin="3"/>
<TextBlock Text="{Binding Version}" FontWeight="Bold" Margin="3"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
做完了ListBox,就可以开始写MainWidow的C#了!
在写C#前,请引用System.ComponentModel.Composition和Contract。。。
完成后,还要添加几个using:
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
然后呢,我把注释放代码里面了,我实在不想多说。。。
using System;
using System.Windows;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Controls;
namespace MEFLauncher
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
class AddInView
{
[ImportMany(AllowRecomposition =true)]
public IMEFLauncherExtension[] MEFLauncherExtensions { get; set; }
public AddInView()
{
#region 检索特定目录下的插件
var catalog = new DirectoryCatalog
(Environment.CurrentDirectory + "\\AddIns");
#endregion
#region 初始化插件
var container = new CompositionContainer(catalog);
#endregion
#region 报错提醒
try
{
container.ComposeParts(this);
}
catch (ChangeRejectedException ex)
{
MessageBox.Show(ex.Message,"Errow!!!");
}
#endregion
}
#region 获取UI
#region GetHomePageUI
public FrameworkElement GetHomepageUI(int HomepageUI)
{
return MEFLauncherExtensions[HomepageUI].GetHomepageUI();
}
#endregion
#region GetIcon
//TODO
#endregion
#endregion
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#region 定义ListBox要显示什么
var adnview = new AddInView();
MyAddIns.ItemsSource = adnview.MEFLauncherExtensions;
#endregion
}
private void MyAddIns_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lists = sender as ListBox;
int index = lists.SelectedIndex;
var MEFLauncherExtension = lists.ItemsSource as IMEFLauncherExtension[];
TabItem newTabPage = new TabItem();
newTabPage.Header = MEFLauncherExtension[index].Title;
newTabPage.Content = MEFLauncherExtension[index].GetHomepageUI();
Homepage.Items.Add(newTabPage);
}
}
}
恭喜你,已经成功了一半!另外一半,我抽时间写写。。。