在 C# 中,什么以及如何在 ErrorListProvider(IServiceProvider isp) 中传递 IServiceProvider?
In C#, what and how to pass IServiceProvider in ErrorListProvider(IServiceProvider isp)?
我正在创建一个 vsix 项目,我需要在错误列表 window 中添加一些自定义错误。现在我卡在了 IServiceProvider
。它包含什么以及为什么我需要它以及我如何才能得到它?在我的代码中,我需要使用服务提供商进行初始化。我怎样才能做到这一点?以下是代码:
internal class ErrorListManager
{
public static ErrorListProvider errorListProvider;
public static void Initialize(IServiceProvider serviceProvider)
{
errorListProvider = new ErrorListProvider(serviceProvider);
}
public static void AddError(string errorMsg) {
AddTask(errorMsg, TaskErrorCategory.Error);
}
private static void AddTask(string errorMsg, TaskErrorCategory category)
{
errorListProvider.Tasks.Add(new ErrorTask
{
Category = TaskCategory.User,
ErrorCategory = category,
Text = errorMsg
});
}
}
请帮忙。我是 C# 和 VSIX 的初学者。谢谢!
您的包裹是服务商。它的基础 class AsyncPackage 派生自实现 OLE.Interop.IServiceProvider 和 System.IServiceProvider 的包 class(是的,VSX 令人困惑,并且有两个 IServiceProvider 接口)。所以,从你的包 class 你会调用 ErrorListManager.Initialize(this).
就是说,您可能希望对 ErrorList 使用 VS 2015 及更高版本的新 API。您使用的旧方法不允许您向 "Code" 等列添加值。请在此处查看示例:
VSSDK-Extensibility-Samples/ErrorList: https://github.com/microsoft/VSSDK-Extensibility-Samples/tree/master/ErrorList
madskristensen/WebAccessibilityChecker: https://github.com/madskristensen/WebAccessibilityChecker/tree/master/src/ErrorList
madskristensen/TaskOutputListener: https://github.com/madskristensen/TaskOutputListener/blob/master/src/ErrorListProvider/ErrorListProvider.cs
要在 VS 错误列表 中写入内容,您需要使用 ErrorListProvider。在我的实现中,我继承自 ErrorListProvider 并为 "ErrorWindowController" 实现我自己的行为。代码如下所示(阅读代码注释以获取更多信息):
public class ErrorWindowController : ErrorListProvider
{
#region Constructor
/// <summary>
/// Instance Constructor
/// </summary>
/// <param name="aServiceProvider"></param>
public ErrorWindowController(IServiceProvider aIServiceProvider) : base(aIServiceProvider)
{
}
#endregion
#region Public Methods
// Use this to add a collection of custom errors in VS Error List
public void AddErrors(IEnumerable<ErrorModel> aErrors)
{
SuspendRefresh();
foreach (ErrorModel error in aErrors)
{
ErrorTask errorTask = new ErrorTask
{
ErrorCategory = error.Category,
Document = error.FilePath,
Text = error.Description,
Line = error.Line - 1,
Column = error.Column,
Category = TaskCategory.BuildCompile,
Priority = TaskPriority.High,
HierarchyItem = error.HierarchyItem
};
errorTask.Navigate += ErrorTaskNavigate;
Tasks.Add(errorTask);
}
BringToFront();
ResumeRefresh();
}
// Remove all the errors from Error List which are depending of a project and this specific project is closed
// Or remove all the errors from Error List when the VS solution is closed
public void RemoveErrors(IVsHierarchy aHierarchy)
{
SuspendRefresh();
for (int i = Tasks.Count - 1; i >= 0; --i)
{
var errorTask = Tasks[i] as ErrorTask;
aHierarchy.GetCanonicalName(Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, out string nameInHierarchy);
errorTask.HierarchyItem.GetCanonicalName(Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, out string nameErrorTaskHierarchy);
if (nameInHierarchy == nameErrorTaskHierarchy)
{
errorTask.Navigate -= ErrorTaskNavigate;
Tasks.Remove(errorTask);
}
}
ResumeRefresh();
}
// Remove all the errors from the Error List
public void Clear()
{
Tasks.Clear();
}
#endregion
#region Private Methods
// This is optional
// Add navigation for your errors.
private void ErrorTaskNavigate(object sender, EventArgs e)
{
ErrorTask objErrorTask = (ErrorTask)sender;
objErrorTask.Line += 1;
bool bResult = Navigate(objErrorTask, new Guid(EnvDTE.Constants.vsViewKindCode));
objErrorTask.Line -= 1;
}
#endregion
}
ErrorModel非常简单:
public class ErrorModel
{
#region Properties
public string FilePath { get; set; }
public int Line { get; set; }
public int Column { get; set; }
public TaskErrorCategory Category { get; set; }
public string Description { get; set; }
public IVsHierarchy HierarchyItem { get; set; }
#endregion
}
编码愉快!
我正在创建一个 vsix 项目,我需要在错误列表 window 中添加一些自定义错误。现在我卡在了 IServiceProvider
。它包含什么以及为什么我需要它以及我如何才能得到它?在我的代码中,我需要使用服务提供商进行初始化。我怎样才能做到这一点?以下是代码:
internal class ErrorListManager
{
public static ErrorListProvider errorListProvider;
public static void Initialize(IServiceProvider serviceProvider)
{
errorListProvider = new ErrorListProvider(serviceProvider);
}
public static void AddError(string errorMsg) {
AddTask(errorMsg, TaskErrorCategory.Error);
}
private static void AddTask(string errorMsg, TaskErrorCategory category)
{
errorListProvider.Tasks.Add(new ErrorTask
{
Category = TaskCategory.User,
ErrorCategory = category,
Text = errorMsg
});
}
}
请帮忙。我是 C# 和 VSIX 的初学者。谢谢!
您的包裹是服务商。它的基础 class AsyncPackage 派生自实现 OLE.Interop.IServiceProvider 和 System.IServiceProvider 的包 class(是的,VSX 令人困惑,并且有两个 IServiceProvider 接口)。所以,从你的包 class 你会调用 ErrorListManager.Initialize(this).
就是说,您可能希望对 ErrorList 使用 VS 2015 及更高版本的新 API。您使用的旧方法不允许您向 "Code" 等列添加值。请在此处查看示例:
VSSDK-Extensibility-Samples/ErrorList: https://github.com/microsoft/VSSDK-Extensibility-Samples/tree/master/ErrorList
madskristensen/WebAccessibilityChecker: https://github.com/madskristensen/WebAccessibilityChecker/tree/master/src/ErrorList
madskristensen/TaskOutputListener: https://github.com/madskristensen/TaskOutputListener/blob/master/src/ErrorListProvider/ErrorListProvider.cs
要在 VS 错误列表 中写入内容,您需要使用 ErrorListProvider。在我的实现中,我继承自 ErrorListProvider 并为 "ErrorWindowController" 实现我自己的行为。代码如下所示(阅读代码注释以获取更多信息):
public class ErrorWindowController : ErrorListProvider
{
#region Constructor
/// <summary>
/// Instance Constructor
/// </summary>
/// <param name="aServiceProvider"></param>
public ErrorWindowController(IServiceProvider aIServiceProvider) : base(aIServiceProvider)
{
}
#endregion
#region Public Methods
// Use this to add a collection of custom errors in VS Error List
public void AddErrors(IEnumerable<ErrorModel> aErrors)
{
SuspendRefresh();
foreach (ErrorModel error in aErrors)
{
ErrorTask errorTask = new ErrorTask
{
ErrorCategory = error.Category,
Document = error.FilePath,
Text = error.Description,
Line = error.Line - 1,
Column = error.Column,
Category = TaskCategory.BuildCompile,
Priority = TaskPriority.High,
HierarchyItem = error.HierarchyItem
};
errorTask.Navigate += ErrorTaskNavigate;
Tasks.Add(errorTask);
}
BringToFront();
ResumeRefresh();
}
// Remove all the errors from Error List which are depending of a project and this specific project is closed
// Or remove all the errors from Error List when the VS solution is closed
public void RemoveErrors(IVsHierarchy aHierarchy)
{
SuspendRefresh();
for (int i = Tasks.Count - 1; i >= 0; --i)
{
var errorTask = Tasks[i] as ErrorTask;
aHierarchy.GetCanonicalName(Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, out string nameInHierarchy);
errorTask.HierarchyItem.GetCanonicalName(Microsoft.VisualStudio.VSConstants.VSITEMID_ROOT, out string nameErrorTaskHierarchy);
if (nameInHierarchy == nameErrorTaskHierarchy)
{
errorTask.Navigate -= ErrorTaskNavigate;
Tasks.Remove(errorTask);
}
}
ResumeRefresh();
}
// Remove all the errors from the Error List
public void Clear()
{
Tasks.Clear();
}
#endregion
#region Private Methods
// This is optional
// Add navigation for your errors.
private void ErrorTaskNavigate(object sender, EventArgs e)
{
ErrorTask objErrorTask = (ErrorTask)sender;
objErrorTask.Line += 1;
bool bResult = Navigate(objErrorTask, new Guid(EnvDTE.Constants.vsViewKindCode));
objErrorTask.Line -= 1;
}
#endregion
}
ErrorModel非常简单:
public class ErrorModel
{
#region Properties
public string FilePath { get; set; }
public int Line { get; set; }
public int Column { get; set; }
public TaskErrorCategory Category { get; set; }
public string Description { get; set; }
public IVsHierarchy HierarchyItem { get; set; }
#endregion
}
编码愉快!