右键菜单可以弹出,但无法触发事件
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if(connectMode == ext_ConnectMode.ext_cm_UISetup)
{
......try
{
......//添加自定义菜单栏
CommandBar CodeWinCommandBar = ((CommandBars)_applicationObject.CommandBars)["Code Window"];
CommandBarPopup MyPopupMenuBar = (CommandBarPopup)CodeWinCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true);
MyPopupMenuBar.Caption = "自定义菜单栏";
//添加自定义菜单项
CommandBarButton MyPopupMenuItem = MyPopupMenuBar.Controls.Add(MsoControlType.msoControlButton, System.Reflection.Missing.Value, System.Reflection.Missing.Value, 1, true) as CommandBarButton;
MyPopupMenuItem.Caption = "我的菜单项";
//为菜单项订阅单击事件
var MyPopupMenuItemEvents = _applicationObject.Events.get_CommandBarEvents(MyPopupMenuItem) as CommandBarEvents;
MyPopupMenuItemEvents.Click += new _dispCommandBarControlEvents_ClickEventHandler(MyPopupMenuItem_Click );}
catch(System.ArgumentException)
{}}}private void MyPopupMenuItem_Click(object CommandBarControl, ref bool Handled, ref bool CancelDefault)
{
MessageBox.Show("I am being Clicked!");
}
test
和我一样,后面找到原因:
“开始时,把menuItemHandler定义局部变量,点击BuildTest菜单项时,有时不执行MenuItem_Click方法,有时第一次点击时执行,但再点击就不执行。
后来把menuItemHandler定义为类的成员变量才解决问题,具体原因目前还不清楚。”