Visual Studio Addin - 无法创建子菜单项
我正在尝试为 Visual Studio 2008 插件创建基本菜单结构。到目前为止,我可以创建第一个顶级菜单,在下面的示例中我将其称为 TOPMENU。我正在努力弄清楚如何将子项目添加到顶部菜单。我尝试了多种方法,都无法解决。有人可以帮我完成下面的代码吗?当我尝试创建下面的对象“a2”时,当前出现无效的强制转换异常。
void IDTExtensibility2.OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_ApplicationObject = (DTE2)application;
_AddInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_ApplicationObject.Commands;
CommandBar cbMainMenu = ((CommandBars)_ApplicationObject.CommandBars)["MenuBar"];
try
{
// ROOT MENU
Command cmdProjectManagement = commands.AddNamedCommand2(_AddInInstance, "TOPMENU", "TOPMENU", "",
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
if (cmdProjectManagement != null)
cmdProjectManagement.AddControl(cbMainMenu, cbMainMenu.Controls.Count);
// SUB ITEM
Command cmdCompiledAssemblies = commands.AddNamedCommand2(_AddInInstance, "TOPMENU_CompiledAssemblies", "Compiled Assemblies", String.Empty,
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
CommandBarControl a1 = cbMainMenu.Controls["TOPMENU"];
CommandBarPopup a2 = (CommandBarPopup)a1;
if (cmdCompiledAssemblies != null)
cmdCompiledAssemblies.AddControl(a2.CommandBar, 1);
}
catch (Exception ex)
{
}
}
}
i am trying to create a basic menu structure for a visual studio 2008 addin. So far i can create the first top-level menu which in my example below i have called TOPMENU. I am struggling to figure out how to add child items to TOPMENU. I have tried a variety of ways and cant figure it out. Can someone help me finish off the code below? I am currently getting an invalid cast exception when i try to create object "a2" below.
void IDTExtensibility2.OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_ApplicationObject = (DTE2)application;
_AddInInstance = (AddIn)addInInst;
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_ApplicationObject.Commands;
CommandBar cbMainMenu = ((CommandBars)_ApplicationObject.CommandBars)["MenuBar"];
try
{
// ROOT MENU
Command cmdProjectManagement = commands.AddNamedCommand2(_AddInInstance, "TOPMENU", "TOPMENU", "",
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
if (cmdProjectManagement != null)
cmdProjectManagement.AddControl(cbMainMenu, cbMainMenu.Controls.Count);
// SUB ITEM
Command cmdCompiledAssemblies = commands.AddNamedCommand2(_AddInInstance, "TOPMENU_CompiledAssemblies", "Compiled Assemblies", String.Empty,
true, null, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
(int)vsCommandStyle.vsCommandStyleText, vsCommandControlType.vsCommandControlTypeButton);
CommandBarControl a1 = cbMainMenu.Controls["TOPMENU"];
CommandBarPopup a2 = (CommandBarPopup)a1;
if (cmdCompiledAssemblies != null)
cmdCompiledAssemblies.AddControl(a2.CommandBar, 1);
}
catch (Exception ex)
{
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关如何创建各种菜单和工具栏的完整文档位于此处
。 mztools.com/articles/2005/mz2005003.aspx
Full documentation on how to create all sorts of menu's and toolbar's here..
http://www.mztools.com/articles/2005/mz2005003.aspx