【问题标题】:How to capture Visual Studio commands in a VSPackage Plugin?如何在 VSPackage 插件中捕获 Visual Studio 命令?
【发布时间】:2014-07-30 11:07:02
【问题描述】:

我正在尝试制作一个插件来捕获Visual Studio的edit.copy命令,如果没有选择则跳过它。

我已经安装了 Visual Studio SDK 并创建了一个 VSPackage 项目。我的初始化方法目前如下所示:

private EnvDTE.DTE m_objDTE = null;
protected override void Initialize()
{
    Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
    base.Initialize();

    m_objDTE = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));

    m_objDTE.Events.CommandEvents.BeforeExecute += (
      string Guid,
      int ID,
      Object CustomIn,
      Object CustomOut,
      ref bool CancelDefault
      ) =>
    {
      EnvDTE.Command objCommand;
      string commandName = "";

      objCommand = m_objDTE.Commands.Item(Guid, ID);
      if (objCommand != null)
      {
        commandName = objCommand.Name;

        if (string.IsNullOrEmpty(commandName))
        {
          commandName = "<unnamed>";
        }
      }

      Debug.WriteLine("Before executing command with Guid=" + Guid + " and ID=" + ID + " named " + commandName);


    };

}

打开解决方案时会调用Initialize() 方法,我已将[ProvideAutoLoad(UIContextGuids80.SolutionExists)] 标签添加到我的包中。但是当我做事情时没有调用 BeforeExecute,比如按下 ctrl + c 这是一个命令,使用这段代码我希望所有命令都打印在调试控制台中,为什么没有发生这种情况?

【问题讨论】:

    标签: c# visual-studio plugins visual-studio-2013 vspackage


    【解决方案1】:

    这是我使用的有效代码:

    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.ComponentModel.Design;
    using Microsoft.Win32;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell;
    using EnvDTE;
    
    namespace Dinto.NoCopy
    {
        [PackageRegistration(UseManagedResourcesOnly = true)]
        [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
        [Guid(GuidList.guidNoCopyPkgString)]
        [ProvideAutoLoad(UIContextGuids80.SolutionExists)]
        public sealed class NoCopyPackage : Package
        {
            public NoCopyPackage()
            {
                Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
            }
    
            #region Package Members
    
    
            private EnvDTE.DTE m_objDTE = null;
            private CommandEvents _pasteEvent;
    
            protected override void Initialize()
            {
                Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
                base.Initialize();
    
                m_objDTE = (DTE)GetService(typeof(DTE));
    
                var pasteGuid = typeof(VSConstants.VSStd97CmdID).GUID.ToString("B");
                var pasteID = (int)VSConstants.VSStd97CmdID.Copy;
    
                _pasteEvent = m_objDTE.Events.CommandEvents[pasteGuid, pasteID];
                _pasteEvent.BeforeExecute += CopyRead;
    
            }
            #endregion
    
            private void CopyRead (
              string Guid,
              int ID,
              Object CustomIn,
              Object CustomOut,
              ref bool CancelDefault
              )
            {
              EnvDTE.Command objCommand;
              string commandName = "";
    
    
              objCommand = m_objDTE.Commands.Item(Guid, ID);
              if (objCommand != null)
              {
                commandName = objCommand.Name;
              }
    
              if (commandName.Equals("Edit.Copy"))
              {
                TextSelection textSelection = (TextSelection)m_objDTE.ActiveDocument.Selection;
    
                if (textSelection.IsEmpty)
                {
                  CancelDefault = true;
                }
              }
            }
    
        }
    }
    

    【讨论】:

    • 我看到你直接使用m_objDTE = (DTE)GetService(typeof(DTE)); 没有checking for Zombie state。这是否会导致 DTE 可能变为null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多