1

私はしばらくの間この問題を抱えていて、どこにも解決策を見つけることができません。現在、Visual Studio 2010用のアドイン(C#を使用)を作成しています。VSメニューバーに新しいメニューを追加しました。このメニューには、「ログイン」や「ログアウト」などのコマンドがあります。強制したい動作は、両方のコマンドが表示されることですが、最初は「ログイン」のみが有効になり、「ログアウト」は最初は無効になります。

これは、OnConnection()メソッドの次のコードを使用して実現します。

    LoginCommand = applicationObject.Commands.AddNamedCommand(
                           addInInstance, 
                           LOGIN_NAME,
                           LOGIN_CAPTION,  
                           LOGIN_TOOLTIP, 
                           true, 59, 
                           ref contextUIGuids,
                           (int)(vsCommandStatus.vsCommandStatusSupported | 
                                vsCommandStatus.vsCommandStatusEnabled)
                        );

    LogoutCommand = applicationObject.Commands.AddNamedCommand(
                            addInInstance, 
                            LOGOUT_NAME,
                            LOGOUT_CAPTION, 
                            LOGOUT_TOOLTIP, 
                            true, 59, 
                            ref contextUIGuids,
                            (int)(vsCommandStatus.vsCommandStatusSupported)
                        );

「login」コマンドを発行して正常にログインした場合は、逆に、メニューで「login」コマンドが無効になり、「logout」が有効になるようにします。ログアウトするまでです。

そして、これは私が立ち往生しているところです。コマンドの状態切り替えをどこでどのように正確に実装するかがわかりません。これはQueryStatus()メソッドで処理する必要があると思いますが、このトピックに関するMicrosoftのドキュメントは、あまり役に立たないか、目を見張るものがあります。

4

2 に答える 2

2

AfterExecuteイベントをイベントに追加する必要がありLoginCommandます。OnConnectionメソッドに以下を追加します。

Events events = (EnvDTE.Events) applicationObject.Events;
CommandEvents LoginEvent = events.get_CommandEvents(LoginCommand.Guid, LoginCommand.ID);
cmdEvent.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(LoginEvent_AfterExecute);

LoginEvent_AfterExecuteメソッドを作成します。

private void LoginEvent_AfterExecute(string guid, int id, object customIn, object customOut)
{
    //Delete the LoginCommand from the commands2 object and recreate it
    LoginCommand.Delete();

    LoginCommand = applicationObject.Commands.AddNamedCommand(
                       addInInstance, 
                       LOGIN_NAME,
                       LOGIN_CAPTION,  
                       LOGIN_TOOLTIP, 
                       true, 59, 
                       ref contextUIGuids,
                       (int)(vsCommandStatus.vsCommandStatusSupported)
                    );

    //Delete the LogoutCommand and recreate it
    LogoutCommand.Delete();  

    LogoutCommand = applicationObject.Commands.AddNamedCommand(
                        addInInstance, 
                        LOGOUT_NAME,
                        LOGOUT_CAPTION, 
                        LOGOUT_TOOLTIP, 
                        true, 59, 
                        ref contextUIGuids,
                        (int)(vsCommandStatus.vsCommandStatusSupported| 
                            vsCommandStatus.vsCommandStatusEnabled)
                    );

}

資力:

于 2012-06-14T12:02:58.867 に答える
1

さて、私は解決策を見つけましたが、それがエレガントかどうかはよくわかりません。コマンド(例LoginCommand)が実行された後、QueryStatus()メソッドは複数回呼び出されますが、の値は異なりますcommandName

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
    {
        if(neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
        {
            if (loginOkay == 0)
            {
                if (commandName == addInInstance.ProgID + "." + LOGIN_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported;
                }
                if (commandName == addInInstance.ProgID + "." + LOGOUT_NAME ||
                    commandName == addInInstance.ProgID + "." + LOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + UNLOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKIN_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKOUT_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                }
            }
            else if (loginOkay == 1)
            {
                if (commandName == addInInstance.ProgID + "." + LOGIN_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                }
                if (commandName == addInInstance.ProgID + "." + LOGOUT_NAME ||
                    commandName == addInInstance.ProgID + "." + LOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + UNLOCK_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKIN_NAME ||
                    commandName == addInInstance.ProgID + "." + CHECKOUT_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported;
                }
            }
            else
            {
                status = vsCommandStatus.vsCommandStatusUnsupported;
            }
        }
    }

とにかく、 Schaliasosに助けてくれてありがとう。私はあなたの答えに投票したいのですが、私は評判ポイントに遅れをとっているので、私はできません。

于 2012-06-15T11:05:00.010 に答える