1、创建空项目
2、创建 Appliction 项目
【MAF.AddInApplication】
前台代码
<Window x:Class="MAF.AddInApplication.MainWindow"
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"
xmlns:local="clr-namespace:MAF.AddInApplication"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="1000">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Background="#F7F9FA"/>
<ListBox Name="lsAddIn">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="3" Background="#EEE">
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
<TextBlock Text="{Binding Pulisher}"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
<Button Content="刷新" Width="90" Height="30" Margin="10 0 0 0" Click="Button_Click"/>
<Button Content="获取插件视图" Width="90" Height="30" Margin="10 0 0 0" Click="Button_Click_1"/>
</StackPanel>
<ContentControl Grid.Row="1" Name="mainContentContainer"></ContentControl>
</Grid>
</Grid>
</Window>
3、合同类实现(Contract)
【MAF.AddInApplication】
创建合同接口类 IOperationContrac。(需要添加 [AddInContract] 特性)
using System;
using System.AddIn.Contract;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.Contract
{
[AddInContract]
public interface IOperationContract : IContract
{
//甲方提供的合同接口
int Process();
}
}
4、插件抽象类实现(AddInView)
【MAF.AddInView】
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.AddInView
{
[AddInBase]
public abstract class OperationAddInView
{
//乙方需要履行的义务【方法名不一定相同,可以通过适配器建立两者的联系】
public abstract int ProcessNumber();
}
}
5、Host 抽象类实现(OperationHostView)
【MAF.OperationHostView】'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.OperationHostView
{
public abstract class OperationHostView
{
public abstract int ProcessNumber();
}
}
6、插件适配器(AddInSideAdapter)
【MAF.AddInSideAdapter】'
using MAF.AddInView;
using MAF.Contract;
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.AddInSideAdapter
{
[AddInAdapter]
public class OperationAddInAdapter : ContractBase, IOperationContract
{
private OperationAddInView view;
public OperationAddInAdapter(OperationAddInView view)
{
this.view = view;
}
public int ProcessNumber()
{
return view.ProcessNumber();
}
}
}
6、Host 适配器(HostAdapter)
【MAF.OperationHostAdapter】
用于解决 Host 主类型与合同类的关系
using MAF.Contract;
using MAF.OperationHostView;
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.OperationHostAdapter
{
[HostAdapter]
public class OperationHostAdapter: OperateHostView
{
private IOperationContract contract;
private ContractHandle contractHandle;
public OperationHostAdapter(IOperationContract contract)
{
this.contract = contract;
//防止合同接口在初始化完成后被释放,用于管理接口的生命周期
this.contractHandle = new ContractHandle(contract);
}
public override int ProcessNumber()
{
return contract.ProcessNumber();
}
}
}
7、创建插件 A(ProcessA)
【MAF.ProcessA】
using MAF.AddInView;
using System;
using System.AddIn;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAF.ProcessA
{
/*
* 1、继承插件抽象类【MAF.AddInView】
* 2、设定插件特性【[AddIn("第一个插件", Version = "1.0", Publisher = "MAF", Description = "插件 MAF Demo")]】
*/
[AddIn("第一个插件", Version = "1.0", Publisher = "MAF", Description = "插件 MAF Demo")]
public class ProcessA : OperationAddInView
{
public override int ProcessNumber()
{
return 100;
}
}
}
8、引用设置
将各个项目间相互引用的 dll,杂项属性中的 ”复制本地“ 设置为 false
9、设定输出目录
【MAF.AddInApplication】-- 【../output】
【MAF.OperationHostView】-- 【../output】
【MAF.OperationHostAdapter】 -- 【../output/HostSideAdapters】
【MAF.Contract】 -- 【../output/Contracts】
【MAF.AddInSideAdapter】 -- 【../output/AddInSideAdapters】
【MAF.AddInView】 -- 【../output/AddInViews】
【MAF.ProcessA】 -- 【../output/AddIns/MAF.ProcessA】