VS2008插件添加到菜单
我使用此代码将一个项目添加到代码窗口右键菜单:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我记得在其他地方看到过这个问题,原因是 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.
我之前没有为 VS.NET 2008 编写过插件,但是看看您的方法中有什么可用的:
检查 ext_cm_UISetup?
另外,在您的 try 块中,您应该能够使用资源管理器...
以及一个可能在将来有所帮助的方便图表。
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?
also, in your try block you should be able to use the resourcemanager...
And a handy diagram that might help in the future.
http://msdn.microsoft.com/en-us/library/za2b25t3.aspx
尝试将:
... 更改为:
... 并将整个内容包装在:
Try changing:
...to:
...and wrap the whole thing in: