Visual Studio Addin - 无法创建子菜单项

发布于 2024-10-06 08:55:01 字数 1724 浏览 0 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-10-13 08:55:01

有关如何创建各种菜单和工具栏的完整文档位于此处

。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文