C# onclick委托仅触发一次

C# onclick委托仅触发一次,c#,delegates,outlook-addin,C#,Delegates,Outlook Addin,我正在编写一个outlook加载项,它向outlook添加了一个菜单虽然我为菜单设置了一个操作,但在调用代理一次后,它似乎被删除了—只需单击菜单项。下次用户单击时,它不会到达我的代理。 代码示例: menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add( Office.MsoControlType.msoControlButton, missing, missing, missing, true); menuComma

我正在编写一个outlook加载项,它向outlook添加了一个菜单虽然我为菜单设置了一个操作,但在调用代理一次后,它似乎被删除了—只需单击菜单项。下次用户单击时,它不会到达我的代理。 代码示例:

menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton, missing, missing, missing, true);

menuCommand.Caption = "&Generate weekly...";
menuCommand.Tag = "Generate";
menuCommand.FaceId = 65;

menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_Generate_Click);

menuCommand = (Office.CommandBarButton)cmdBarControl.Controls.Add(
    Office.MsoControlType.msoControlButton, missing, missing, missing, true);

menuCommand.Caption = "&About";
menuCommand.Tag = "About";
menuCommand.FaceId = 65;

menuCommand.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_About_Click);
menuCommand.BeginGroup = true;

要使菜单每次都触发操作,我应该怎么做?

菜单项是局部变量吗, 因为一旦垃圾收集器超出范围,它可能会进行清理


尝试将变量保留在全局对象中。

menuitem是局部变量吗, 因为一旦垃圾收集器超出范围,它可能会进行清理


尝试将变量保留在全局对象中。

我现在看到您正在将同一变量用于第二个按钮。这也将导致第一个按钮成为垃圾收集的候选按钮。我现在看到您正在为第二个按钮使用相同的变量。这也将导致第一个按钮成为垃圾收集的候选按钮。