VS2008插件添加到菜单

发布于 2024-07-30 07:10:17 字数 1602 浏览 1 评论 0原文

我使用此代码将一个项目添加到代码窗口右键菜单:

public void OnConnection(
 object application, 
 ext_ConnectMode connectMode, 
 object addInInst, 
 ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    object[] contextGUIDS = new object[] { };
    Command codeWindowCommand = null;
    CommandBarControl codeWindowButton;
    CommandBar codeCommandBar;
    CommandBars commandBars;

    try
    {
        codeWindowCommand = _applicationObject.Commands.Item(
            _addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0);
    }
    catch
    {
    }

    if (codeWindowCommand == null)
    {
        codeWindowCommand = _applicationObject.Commands.AddNamedCommand(
            _addInInstance, 
            CODEWINDOW_COMMAND_NAME, 
            CODEWINDOW_COMMAND_NAME, 
            "Pastebin selected code", 
            true, 
            18, 
            ref contextGUIDS, 
            (int)vsCommandStatus.vsCommandStatusSupported + 
            (int)vsCommandStatus.vsCommandStatusEnabled);
    }

    commandBars = (CommandBars)_applicationObject.CommandBars;

    codeCommandBar = commandBars["Code Window"];

    codeWindowButton = (CommandBarControl)codeWindowCommand.AddControl(
        codeCommandBar, codeCommandBar.Controls.Count + 1);
    codeWindowButton.Caption = "Text for button";
    codeWindowButton.TooltipText = "Tooltip for button";
}

并将插件设置为自动启动。 然而,每次运行 VS2008 时,它都会在菜单中添加另一个按钮,直到我完全删除该插件。 有人知道我如何解决这个问题吗?

例如,我会将 Command.AddControl() 和后面的内容包装在 if 中,仅当按钮尚不存在时才执行,但我似乎找不到在 API 中检查此内容的方法?

I'm using this code to add an item to the code window right click menu:

public void OnConnection(
 object application, 
 ext_ConnectMode connectMode, 
 object addInInst, 
 ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    object[] contextGUIDS = new object[] { };
    Command codeWindowCommand = null;
    CommandBarControl codeWindowButton;
    CommandBar codeCommandBar;
    CommandBars commandBars;

    try
    {
        codeWindowCommand = _applicationObject.Commands.Item(
            _addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0);
    }
    catch
    {
    }

    if (codeWindowCommand == null)
    {
        codeWindowCommand = _applicationObject.Commands.AddNamedCommand(
            _addInInstance, 
            CODEWINDOW_COMMAND_NAME, 
            CODEWINDOW_COMMAND_NAME, 
            "Pastebin selected code", 
            true, 
            18, 
            ref contextGUIDS, 
            (int)vsCommandStatus.vsCommandStatusSupported + 
            (int)vsCommandStatus.vsCommandStatusEnabled);
    }

    commandBars = (CommandBars)_applicationObject.CommandBars;

    codeCommandBar = commandBars["Code Window"];

    codeWindowButton = (CommandBarControl)codeWindowCommand.AddControl(
        codeCommandBar, codeCommandBar.Controls.Count + 1);
    codeWindowButton.Caption = "Text for button";
    codeWindowButton.TooltipText = "Tooltip for button";
}

and the addin is set to autostart. However each time the run VS2008 it adds another button to the menu until I totally delete the addin. Anyone know how I fix this?

I would for example wrap the Command.AddControl() and later stuff in an if that only executes if the button doesn't already exist, but I can't seem to find a way to check this in the API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

携君以终年 2024-08-06 07:10:18

我记得在其他地方看到过这个问题,原因是 OnConnection 方法可以出于多种原因被多次调用(具有不同的 connectMode 值),因此存在一些技巧(或特殊性,具体取决于您如何看待它以及如何处理)其中大部分你都知道)参与其中。

但是,我不是这个主题的专家,所以这里有一些帮助您的链接:

如何:正确使用 Visual Studio 加载项的 OnConnection 方法

如何:从加载项向 Visual Studio .NET 添加按钮、命令栏和工具栏

HOWTO:控制 Visual Studio 加载项中的命令状态

这些有点太长,无法在这里总结(至少对我来说是这样) ,但他们确实有您需要的信息。

另外,这里有一个关于编写 VS 插件的文章列表,这可能会非常有帮助:
http://www.mztools.com/resources_vsnet_addins.aspx

HTH。


编辑:Money J 的回答更切题,我想,基本上是对您需要做的事情的非常简短的总结,如果这就是您所追求的一切 - 那就太好了。 但是,我相信我提供的链接页面中包含的信息非常有用,因此您可能也想阅读该信息。

I remember seeing this problem elsewhere, and the reason was that the OnConnection method can be called at multiple times for multiple reasons (with different values of connectMode), so there is some trickery (or peculiarities, depending on how you look at it and how much of this you know) involved.

However, I am not an expert on this topic, so here are some links that will help you:

HOWTO: Use correctly the OnConnection method of a Visual Studio add-in

HOWTO: Adding buttons, commandbars and toolbars to Visual Studio .NET from an add-in

HOWTO: Controlling the state of command in a Visual Studio add-in

Those are somewhat too long to just summarize here (at least it seems like that to me), but they do have the information you need.

Also, here is a list of articles on writing VS addins, which will probably be very helpful:
http://www.mztools.com/resources_vsnet_addins.aspx

HTH.


EDIT: Money J's answer is a bit more to the point, I suppose, and is basically a very short summary of what you need to do, and if that's all you're after - great. However, I believe that the information contained on the pages I provided the links to is very useful, so you might want to read that as well.

清音悠歌 2024-08-06 07:10:18

我之前没有为 VS.NET 2008 编写过插件,但是看看您的方法中有什么可用的:

检查 ext_cm_UISetup?

 if(connectMode == ext_ConnectMode.ext_cm_UISetup)
    {

另外,在您的 try 块中,您应该能够使用资源管理器...

 ResourceManager resourceManager = new     
          ResourceManager("MyAddin1.CommandBar",  
          Assembly.GetExecutingAssembly());
        CultureInfo cultureInfo = new 
          System.Globalization.CultureInfo
          (_applicationObject.LocaleID);
        string resourceName = String.Concat(cultureInfo.
          TwoLetterISOLanguageName, "Tools");
        toolsMenuName = resourceManager.GetString(resourceName);

以及一个可能在将来有所帮助的方便图表。

http://msdn.microsoft.com/en-us/library/za2b25t3。 ASPX

I haven't written an addin for VS.NET 2008 before, but seeing what you have available on your method:

Check for ext_cm_UISetup?

 if(connectMode == ext_ConnectMode.ext_cm_UISetup)
    {

also, in your try block you should be able to use the resourcemanager...

 ResourceManager resourceManager = new     
          ResourceManager("MyAddin1.CommandBar",  
          Assembly.GetExecutingAssembly());
        CultureInfo cultureInfo = new 
          System.Globalization.CultureInfo
          (_applicationObject.LocaleID);
        string resourceName = String.Concat(cultureInfo.
          TwoLetterISOLanguageName, "Tools");
        toolsMenuName = resourceManager.GetString(resourceName);

And a handy diagram that might help in the future.

http://msdn.microsoft.com/en-us/library/za2b25t3.aspx

等往事风中吹 2024-08-06 07:10:18

尝试将:

codeWindowCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0); 

... 更改为:

codeWindowCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, -1); 

... 并将整个内容包装在:

#if DEBUG
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
#else
if (connectMode == ext_ConnectMode.ext_cm_Startup || connectMode == ext_ConnectMode.ext_cm_AfterStartup)
#endif
{
    //add-in startup code goes here
}

Try changing:

codeWindowCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, 0); 

...to:

codeWindowCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + CODEWINDOW_COMMAND_NAME, -1); 

...and wrap the whole thing in:

#if DEBUG
if (connectMode == ext_ConnectMode.ext_cm_UISetup)
#else
if (connectMode == ext_ConnectMode.ext_cm_Startup || connectMode == ext_ConnectMode.ext_cm_AfterStartup)
#endif
{
    //add-in startup code goes here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文