ToolStripMenuItems
現在、私は別のものを1つにマージしようとしていMenuStrip
ます。そのため、と呼ばれるインターフェイスを作成しましたIMenu
。これにはToolStripMenuItem
MEFプラグインからのものが含まれ、Main-Window-Menustripにロードする必要があります。問題は、アプリケーションを実行するMenuStrip
と、同じ名前の2つのDropDown要素を取得することです。しかし、私が欲しいのは、MEFプラグインのメニューをダブルエントリなしでアプリケーションのMenuStripにバインドできることです。たとえば、MEFプラグインの優れたメニュー構造を作成するにはどうすればよいですか。
例:メインアプリケーションからこのエントリがあります
ファイル
|->新規
|->保存
|->インポート
|->エクスポート
次に、特定のタイプをインポート/エクスポートするためのプラグインを作成します。そのため、インポート/エクスポートの下のエントリを動的にメニューに追加する必要があります。しかし、どのように?あなたの解決策はどうですか?
ファイル
|->新規
|->保存
|->インポート
|----->Wordへ
|->エクスポート
|----->Wordから
ここに私のコードがあります:最初にプラグインのインターフェース
public interface IMenu
{
ToolStripMenuItem ToolStripItem { get; }
}
これはメニュー項目のプラグインの例です
ヘルプ
|->更新
[Export(typeof(IMenu))]
class UpdateMenuItems : IMenu
{
System.Windows.Forms.ToolStripMenuItem helpMenuItem;
System.Windows.Forms.ToolStripMenuItem updateMenuItem;
public System.Windows.Forms.ToolStripMenuItem ToolStripItem
{
get { return helpMenuItem; }
}
public UpdateMenuItems()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.updateMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.updateMenuItem.Name = "Update";
this.updateMenuItem.Size = new System.Drawing.Size(94, 20);
this.updateMenuItem.Text = "Update";
this.updateMenuItem.MergeIndex = 1;
this.updateMenuItem.MergeAction = System.Windows.Forms.MergeAction.Insert;
this.updateMenuItem.Click += updateMenuItem_Click;
this.helpMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.helpMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.updateMenuItem});
this.helpMenuItem.Name = "aboutToolStripMenuItem";
this.helpMenuItem.Size = new System.Drawing.Size(44, 20);
this.helpMenuItem.MergeAction = System.Windows.Forms.MergeAction.Insert;
this.helpMenuItem.Text = "Help";
}
void updateMenuItem_Click(object sender, EventArgs e)
{
UpdateController update = new UpdateController();
update.Execute();
}
これは、ルート要素「ヘルプ」を持つ別のMEFプラグインです。
[Export(typeof(IMenu))]
class MenuItem : IMenu
{
public ToolStripMenuItem ToolStripItem
{
get { return testItem; }
}
private System.Windows.Forms.ToolStripMenuItem testItem;
private System.Windows.Forms.ToolStripMenuItem unterpunk1;
private System.Windows.Forms.ToolStripMenuItem unterpunk2;
public MenuItem()
{
InitializeComponent();
}
public void InitializeComponent()
{
this.testItem = new System.Windows.Forms.ToolStripMenuItem();
this.testItem.Name = "aboutToolStripMenuItem";
this.testItem.Size = new System.Drawing.Size(94, 20);
this.testItem.Text = "Help";
this.testItem.Click += testItem_Click;
unterpunk1 = new ToolStripMenuItem();
unterpunk1.Text = "Speichern";
unterpunk1.MergeAction = MergeAction.Insert;
unterpunk1.MergeIndex = 1;
testItem.DropDownItems.Add(unterpunk1);
unterpunk2 = new ToolStripMenuItem();
unterpunk2.Text = "Prüfen";
unterpunk2.MergeAction = MergeAction.Insert;
unterpunk2.MergeIndex = 2;
testItem.DropDownItems.Add(unterpunk2);
}
void testItem_Click(object sender, EventArgs e)
{
//
}
私のメインフォームでは、すべてをMenuStripに追加します。
foreach (var item in MenuItems)
{
this.menuStrip1.Items.Add(item.ToolStripItem);
}
自動的にToolStripMenuItems
マージしませんか?