我目前正在将以前的VBE-Macros移植到Visual Studio2012。由于VBE不再可用,您需要实现自定义外接程序。到目前为止没有问题。
那就是我创建C#插件的地方,它实现了IDTExtensibility2和IDTCommandTarget。在OnConnection方法中,我注册了应在Visual Studio启动时执行的命令,方法是:
devenv [parameters...] /Command:AddIn.Connect.MakeMeProud
我实现了(遇到各种问题之后)一个帮助程序,该程序在Visual Studio中注册了该命令:
private const String CommandMain = "AddIn";
private void registerCommand(String Name, ref object[] ctguid)
{
String cmd = CommandMain + ".Connect." + Name;
int disableFlags = (int)vsCommandStatus.vsCommandStatusSupported +
(int)vsCommandStatus.vsCommandStatusEnabled;
_applicationObject.Commands.AddNamedCommand(_addInInstance, cmd, Name, Name, false, 0, ref ctguid, disableFlags);
}
private void registerCommands()
{
object[] contextGUIDS = new object[]{};
registerCommand("MakeMeProud", ref contextGUIDS);
}
private void unregisterCommands()
{
foreach (Command cmd in (_applicationObject.Commands))
{
try
{
if (cmd.Name.StartsWith(CommandMain + ".Connect."))
cmd.Delete();
}
catch { }
}
}
删除现有命令后,将在OnConnection中调用函数registerCommands():
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
if ((ext_ConnectMode.ext_cm_AfterStartup == connectMode) ||
(ext_ConnectMode.ext_cm_Startup == connectMode) ||
(ext_ConnectMode.ext_cm_CommandLine == connectMode))
{
unregisterCommands();
registerCommands();
}
}
但是,当我加载Addin时,Visual Studio会在调用AddNamedCommand时抱怨System.InvalidArgumentException(当我使用AddNamedCommand2时也会发生这种情况)。
有什么建议?
我不想在“工具”中有任何条目,而只是从命令行和“命令窗口”中运行它。
:(
发现thr错误。
您只需要按名称(不带类)注册命令。在Query and Exec中,您也可以比较Classname。
该错误消息不是很有帮助,但是使用调试器检查另一个Addin很有帮助。
干杯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句