带MEF的Winforms [英] Winforms with MEF
问题描述
我有一个使用不同选项卡的winforms应用程序.我想使用MEF来添加更多在启动时导入的选项卡.我很难弄清楚该怎么做.
I have a winforms App that uses different tabs. I would like to use MEF to be able to add more tabs that are imported at startup. I am having a hard time figuring out how to go about doing this.
这就是我所做的.
我选择了主要的winforms类并将其缩减,以便其中只有一个TabControl,我通过一个接口向每个TabPage公开.然后,我还创建了第二个接口ITab,与MEF一起使用以获取标签页,然后将其添加到主tabcontrol中.要创建一个新的标签页,我只需添加一个新表单,然后向其中添加一个tabcontrol并设计标签页.我将ITab界面添加到新表单中,并添加以下方法将页面移至主表单.
I took the main winforms class and striped it down so that there is only a TabControl in it which I expose to each each TabPage through an interface. I then also create a second interface ITab which I use with MEF to get the tabpage and then add it to to the main tabcontrol. To create a new tab page I then just add a new form and then add a tabcontrol to it and design the tab pages. I add the ITab interface to the new form and add the following method which moves the pages to the main form.
public void MoveTabPages(IForm fm)
{
while (this.tabControl1.Controls.Count > 0)
{
fm.tab.Controls.Add(this.tabControl1.Controls[0]);
}
}
事件委托和所有这些好东西只要它们仅引用表单类中的内容,就可以工作.
Events delegates and all of that good stuff work as long as they only reference what is in their form class.
这是完整的代码.
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace Winforms_Mef
{
public interface IForm
{
TabControl tab { get; }
}
public interface ITab
{
void MoveTabPages(IForm fm);
}
public partial class Form1 : Form,IForm
{
private CompositionContainer _container;
[Import]
public IEnumerable Tabs { get; set; }
public TabControl tab
{
get { return tabControl1; }
}
public Form1()
{
Compose();
InitializeComponent();
foreach (ITab tab in Tabs)
{
tab.MoveTabPages(this);
}
}
private void Compose()
{
var catalog =new AssemblyCatalog(typeof(ITab).Assembly);
var batch = new CompositionBatch();
batch.AddPart(this);
_container =new CompositionContainer(catalog);
_container.Compose(batch);
}
}
}
//Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Composition;
namespace Winforms_Mef
{
[Export(typeof(ITab))]
public partial class Form2 : Form,ITab
{
public Form2()
{
InitializeComponent();
}
public void MoveTabPages(IForm fm)
{
while (this.tabControl1.Controls.Count > 0)
{
fm.tab.Controls.Add(this.tabControl1.Controls[0]);
}
}
}
}
推荐答案
在继续之前,我认为需要清理您的Compose方法.为什么要将容器和目录添加到批处理中?
Before moving on I think your Compose method needs to be cleaned up. Why are you adding the container and catalog into the batch?
batch.AddExportedObject(_container);
batch.AddExportedObject(catalog);
AddExportedObject 用于将已有的对象实例添加为导出对象,尝试将容器和目录用作导出对象并没有多大意义
AddExportedObject is used to add a pre-existing object instance as an export and it doesn't make much sense trying to use the container and catalog as exports
privat void Compose()
{
var catalog =
new AssemblyCatalog(typeof(ITab).Assembly);
var batch =
new CompositionBatch();
batch.AddPart(this);
var container =
new CompositionContainer(catalog);
container.Compose(batch);
}
这篇关于带MEF的Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!