VSIX ErrorListProvider 任务列表为空

VSIX ErrorListProvider Tasks List is Empty

我正在尝试使用 SDK 创建一个 Visual Studio 扩展,创建一个 VSIX 包。我正在使用 Microsoft Visual Studio 2019 预览版;版本 16.7.0 预览版 1.0

private readonly AsyncPackage package;

private void Test1()
{
    ThreadHelper.ThrowIfNotOnUIThread();

    var ivsSolution = (IVsSolution)Package.GetGlobalService(typeof(IVsSolution));
    var dte = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(EnvDTE.DTE));
    var errorListProvider = new ErrorListProvider(package);
    var tasks = errorListProvider.Tasks.Count;
}

最后一行的计算结果为零,即使错误 window 显示了很多错误。 我究竟做错了什么?

谢谢,

ErrorListProvider 向错误列表工具窗口提供错误。要显示(或访问)现有错误,您可以按照 Klaus 的建议使用 ToolWindows.ErrorList。文档中的示例代码并不完全正确。它看起来像是从一个自定义的插件项目中提取出来的,该项目现在已经过时了。对于那些较旧的插件项目,applicationObject 通常指的是 DTE 接口。

从 VSSDK 包中,您将执行类似于以下的操作:

using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using EnvDTE80;

namespace GetErrorsDemo
{

    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid(GetErrorsDemoPackage.PackageGuidString)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    public sealed class GetErrorsDemoPackage : AsyncPackage
    {
        public const string PackageGuidString = "90adc626-67bd-42d5-babc-6e4c5aa6e351";
        public static readonly Guid CommandSet = new Guid("5a7f888e-8767-4a4a-a06b-1b06c4f1e3f4");
        public const int ErrorsListCommand = 0x0100;

        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            // add our menu handler
            OleMenuCommandService commandService = await this.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
            Assumes.Present(commandService);
            var menuCommandID = new CommandID(CommandSet, ErrorsListCommand);
            var menuItem = new MenuCommand(this.OnErrorsListCommand, menuCommandID);
            commandService.AddCommand(menuItem);
        }

        private void OnErrorsListCommand(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            DTE2 dte = (DTE2)GetService(typeof(SDTE));
            Assumes.Present(dte);
            ErrorList errorList = dte.ToolWindows.ErrorList;
            for (int i = 1; i <= errorList.ErrorItems.Count; i++)
            {
                string msg = string.Format("Description: {0}", errorList.ErrorItems.Item(i).Description);
                VsShellUtilities.ShowMessageBox(this, msg, "GetErrors Demo", OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
            }
        }
    }
}