如何在默认情况下检查按钮?

时间:2016-06-12 23:35:44

标签: visual-studio-extensions

这是我的按钮:

  <Button guid="guidTranslationAutomatorCommandPackageCmdSet" id="ToggleCommand" priority="0x0000" type="Button">
    <Parent guid="guidTranslationAutomatorCommandPackageCmdSet" id="SubMenuGroup" />
    <CommandFlag>TextChanges</CommandFlag>
    <CommandFlag></CommandFlag>
    <Strings>
      <CommandName>cmdidTestSubCommand</CommandName>
      <ButtonText>Auto (On)</ButtonText>
    </Strings> 
  </Button>

一旦点击它,我就可以切换它的检查状态:

    private void MenuItemCallback(object sender, EventArgs en)
    {
        var command = sender as OleMenuCommand;
        var mcs = this.ServiceProvider.GetService(typeof(IMenuCommandService))
            as OleMenuCommandService;
        var newCmdID = new CommandID(new Guid(TranslationAutomatorCommand.guidTranslationAutomatorCommandPackageCmdSet), command.CommandID.ID);
        MenuCommand mc = mcs.FindCommand(newCmdID);
        if (mc != null)
        {
            mc.Checked = true;
        }
    }

但我希望检查初始状态。我怎么做?它可能是.vsct文件中的内容,但我找不到它。

1 个答案:

答案 0 :(得分:0)

在包初始化函数中检查它。

protected override void Initialize()
    {
        var command = sender as OleMenuCommand;
        var mcs = this.ServiceProvider.GetService(typeof(IMenuCommandService))
        as OleMenuCommandService;
        var newCmdID = new CommandID(new Guid(TranslationAutomatorCommand.guidTranslationAutomatorCommandPackageCmdSet), command.CommandID.ID);
        MenuCommand mc = mcs.FindCommand(newCmdID);
        if (mc != null)
        {
            mc.Checked = true;
        }
}