ModelExplorerTreeContainer.AddCommandHandlers 方法

提供用于从快捷菜单查找、调用和移除菜单命令的方法。

命名空间:  Microsoft.VisualStudio.Modeling.Shell
程序集:  Microsoft.VisualStudio.Modeling.Sdk.Shell.12.0(在 Microsoft.VisualStudio.Modeling.Sdk.Shell.12.0.dll 中)

语法

声明
Public Overridable Sub AddCommandHandlers ( _
    menuCommandService As IMenuCommandService _
)
public virtual void AddCommandHandlers(
    IMenuCommandService menuCommandService
)

参数

备注

此方法调用 ISelectionService 接口。

当处理程序未注册,基类实现将命令处理程序。 这允许派生类重写处理每个命令。 因此,基类,在调用之前,派生类应当添加命令。

示例

本示例将自定义命令添加到快捷菜单。 当用户在生成的设计器的解决方案并右击关系图时,另一个命令,则 命令示例,显示快捷菜单。

在 Commands.vsct 文件,下面的行。包括语句后出现。

#define cmdidMyMenuSample  0x0008

在 Commands.vsct 文件,下面一行在 GENERATED_BUTTONS 后显示。

guidCmdSet:cmdidMyMenuSampe, guidCommonModelingMenu:grpidExplorerMenuGroup, 0x8020, OI_NOID, BUTTON, DIS_DEF, "&Sample Command";

在 VsctComponents 文件夹中,如下 .cs 文件可用。命名空间和方法有一些项目,MenuSample 的名称,在它们。

using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslShell = global::Microsoft.VisualStudio.Modeling.Shell;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using System;
using System.ComponentModel.Design;
using System.Windows.Forms;

namespace MS.MenuSample
{
    internal partial class MenuSampleExplorer
    {

        public override void AddCommandHandlers(System.ComponentModel.Design.IMenuCommandService menuCommandService)
        {
            menuCommandService.AddCommand(new DslShell::DynamicStatusMenuCommand(
                new EventHandler(OnStatusMyMenuSample),
                new EventHandler(OnMenuMyMenuSample),
                Constants.MyMenuSampleCommand));

            base.AddCommandHandlers(menuCommandService);
        }

        private void OnStatusMyMenuSample(object sender, EventArgs e)
        {
            System.ComponentModel.Design.MenuCommand cmd = sender as System.ComponentModel.Design.MenuCommand;
            cmd.Enabled = cmd.Visible = true;
        }

        private void OnMenuMyMenuSample(object sender, EventArgs e)
        {
            MessageBox.Show("Place for you to act when user chooses this menu item");
        }

    }

    internal static partial class Constants
    {
        private const int cmdidMyMenuSample = 0x0008;
        public static readonly CommandID MyMenuSampleCommand = new CommandID(new Guid(MenuSampleCommandSetId), Constants.cmdidMyMenuSample);
    }
}

.NET Framework 安全性

请参阅

参考

ModelExplorerTreeContainer 类

Microsoft.VisualStudio.Modeling.Shell 命名空间