美文网首页
VS2019开发VSIX插件

VS2019开发VSIX插件

作者: Ju1yz | 来源:发表于2020-02-22 15:57 被阅读0次

一. 创建VSIX项目

VS新建项目, 选择VSIX Project


创建VSIX项目

二. 配置VSIX

  1. 打开source.extension.vsixmanifest文件, 填写基本信息


    基本信息
  2. 选择Install Targets, 配置插件适用的VS版本


    配置插件适用的VS版本

VS版本:
VS2019=>16.0
VS2017=>15.0
VS2015=>14.0
VS2013=>12.0

三. 新增Command文件

项目右键添加新建项, 选择Extensibility下的Command


添加Command文件

四. 修改vsct文件

  1. 增加父节点位置(Guid和ID值获取参照第五点)
    <!--选择文件夹的Guid和ID-->
    <GuidSymbol name="guidFolderRightClickCmdSet" value="{D309F791-903F-11D0-9EFC-00A0C911004F}">
      <IDSymbol name="folderRightClickMenu" value="1073" />
    </GuidSymbol>
  1. 修改新增菜单的所属父节点
    <Groups>
      <Group guid="guidRestServiceToolPackageCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidFolderRightClickCmdSet" id="folderRightClickMenu" />
        <!--<Parent guid="guidSHLMainMenu" id="IDG_VS_PROJ_FOLDER"/>-->
      </Group>
    </Groups>

五. 获取VS菜单的Guid和ID

  1. 打开注册表, 输入路径"计算机\HKEY_CURRENT_USER\Software\Microsoft\VisualStudio"回车, 找到使用的VS版本, 在下面新建项General, 然后新建DWORD值EnableVSIPLogging为1


    image.png
  2. 用VS打开任意项目, 找到自己想获取Guid和ID的菜单位置, 按住Ctrl+Shift, 鼠标右键菜单, 弹出VSDebug Message窗口(如获取项目引用的Guid和ID如下)


    项目引用菜单Guid和ID

六. 获取鼠标点击相关对象值

  1. 打开**Package.cs文件, 增加如下获取DTE2实例:
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress) {
            // When initialized asynchronously, the current thread may be a background thread at this point.
            // Do any initialization that requires the UI thread after switching to the UI thread.
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            // 获取DTE2实例
            var dte = await GetServiceAsync(typeof(DTE)) as DTE2;
            await Command1.InitializeAsync(this);
        }
  1. 打开Command.cs文件, 增加如下内容:
        /// <summary>
        /// VS Package that provides this command, not null.
        /// </summary>
        private readonly AsyncPackage package;

        private static DTE2 _dte;
        public static async Task InitializeAsync(AsyncPackage package, DTE2 dte) {
            // Switch to the main thread - the call to AddCommand in Command1's constructor requires
            // the UI thread.
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);

            OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
            Instance = new Command1(package, commandService);

            _dte = dte;
        }
        private void Execute(object sender, EventArgs e) {
            var selectedItems = _dte.ToolWindows.SolutionExplorer.SelectedItems as UIHierarchyItem[];
            if (selectedItems == null || selectedItems.Length == 0) {
                return;
            }
            // 选择对象
            var selectedItem = selectedItems[0]?.Object as ProjectItem;
            if (selectedItem == null) {
                return;
            }
            // 项目
            var project = selectedItem.ContainingProject;
            if (project == null) {
                return;
            }
            // 项目名
            var projectName = project.Name;

            ThreadHelper.ThrowIfNotOnUIThread();
            string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
            string title = "Command1";

            // Show a message box to prove we were here
            VsShellUtilities.ShowMessageBox(
                this.package,
                projectName,
                title,
                OLEMSGICON.OLEMSGICON_INFO,
                OLEMSGBUTTON.OLEMSGBUTTON_OK,
                OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
        }

七. 运行

F5运行新建项目进行测试

相关文章

网友评论

      本文标题:VS2019开发VSIX插件

      本文链接:https://www.haomeiwen.com/subject/ylplqhtx.html