我正在尝试使用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;
}
即使错误窗口显示许多错误,最后一行的计算结果也为零。我究竟做错了什么?
谢谢,
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);
}
}
}
}