简体   繁体   中英

Cannot register Command in Visual Studio 2012 with AddNamedCommand2

I'm currently working on porting prior VBE-Macros to Visual Studio 2012. As the VBE is no longer available, you need to implement a custom Addin. So far no problems.

Thats where I've created a C# Addin, with implements IDTExtensibility2 and IDTCommandTarget. In the OnConnection method I've registering the Command which should be executed on start of Visual Studio by

devenv [parameters...] /Command:AddIn.Connect.MakeMeProud

I've implemented (after running into various problems) a helper routine, which registers the command within 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 { }
        }
    }

The function registerCommands() is called in OnConnection after deleting existing commands:

    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();
        }                    
    }

But when I load the Addin, Visual Studio complains about a System.InvalidArgumentException when calling AddNamedCommand (same happens when I use AddNamedCommand2).

Any suggestions?

I don't want to have any entries in Tools, just running it from command line and from the Command Window.

:(

Found thr error.

You need to Register the command just by Name, without Class. In Query and Exec you compare the Classname too.

The error message is not quite helpful, but examinating another Addin with the Debugger helped a lot.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM