Visual Studio加载项始终将自身添加到菜单中

时间:2013-09-30 10:04:10

标签: c# visual-studio-2010 visual-studio

我创建了一个visual studio插件,但是当我一次又一次打开visual studio时它会自我添加。现在我的视觉工作室有20个菜单:)

如何检查是否已添加?

我在Connect.cs [OnStartupComplete方法]

上使用以下代码

我观看了有关添加菜单http://msdn.microsoft.com/en-us/vstudio/bb614548.aspx

的视频
Command command = null;
CommandBarControl commandBarControl;
CommandBar commandBarASPX, commandBarCS;
CommandBars commandBars;

try
{
    try
    {
        command = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + AddInName);
    }
    catch
    {

    }

    if (command == null)
    {
        command = _applicationObject.Commands.AddNamedCommand(_addInInstance, AddInName, AddInButtonName, AddInButtonTooltip, true, 528, null, (int)vsCommandStatus.vsCommandStatusEnabled | (int)vsCommandStatus.vsCommandStatusSupported);
    }
    else
    {
        commandBars = _applicationObject.CommandBars as CommandBars;

        commandBarASPX = commandBars["ASPX Context"];
        commandBarControl = command.AddControl(commandBarASPX, commandBarASPX.Controls.Count + 1) as CommandBarControl;

        commandBarCS = commandBars["Code Window"];
        commandBarControl = command.AddControl(commandBarCS, commandBarCS.Controls.Count + 1) as CommandBarControl;
    }
}
catch (Exception exp)
{
    MessageBox.Show(exp.Message.ToString());
}

1 个答案:

答案 0 :(得分:0)

似乎您每次启动完成时都会在“ASPX Context”命令栏和“Code Window”命令栏的命令列表末尾添加命令。

您应该在命令栏中的固定位置添加命令,例如,在位置1处,以便每次只有在命令不存在时才添加命令。

所以你可以尝试以下方法:

commandBarControl = command.AddControl(commandBarASPX, 1) as CommandBarControl;

// ...

commandBarControl = command.AddControl(commandBarCS, 1) as CommandBarControl;