问题描述
我正在编写一个自定义工具,目前我正在按照自己的意愿执行其功能.如果出现问题,我希望能够写入Visual Studio. (格式错误的代码或其他内容).
I am writing a custom tool and I currently have it doing what I want as far as functionality. I would like to be able to write to Visual Studio if something goes wrong. (Incorrectly formatted code or whatever).
对此有任何标准吗?现在,我基本上可以强制该工具失败,并且Visual Studio发出警告,告知它已经这样做了.我想要输出"窗口中的类别,其中包含要发送的所有结果消息.我还可以在错误列表"窗口中接受更具描述性的任务/警告.
Are there any standards for this? Right now I basically can force the tool to fail and Visual Studio puts in a warning that it has done so. I'd like a category in the Output window with any resulting messages I want to send. I could also live with a more descriptive task/warning in the Error list window.
推荐答案
输出窗口
要写入Visual Studio中的常规"输出窗口,您需要执行以下操作:
Output Window
To write to the "General" output window in Visual Studio, you need to do the following:
IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;
Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane; // P.S. There's also the GUID_OutWindowDebugPane available.
IVsOutputWindowPane generalPane;
outWindow.GetPane( ref generalPaneGuid , out generalPane );
generalPane.OutputString( "Hello World!" );
generalPane.Activate(); // Brings this pane into view
但是,如果要写入自定义窗口,则需要执行以下操作:
If, however, you want to write to a custom window, this is what you need to do:
IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;
// Use e.g. Tools -> Create GUID to make a stable, but unique GUID for your pane.
// Also, in a real project, this should probably be a static constant, and not a local variable
Guid customGuid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789");
string customTitle = "Custom Window Title";
outWindow.CreatePane( ref customGuid, customTitle, 1, 1 );
IVsOutputWindowPane customPane;
outWindow.GetPane( ref customGuid, out customPane);
customPane.OutputString( "Hello, Custom World!" );
customPane.Activate(); // Brings this pane into view
有关 IVsOutputWindow 和在MSDN上可以找到 IVsOutputWindowPane .
Details on IVsOutputWindow and IVsOutputWindowPane can be found on MSDN.
要向错误列表中添加项目,IVsSingleFileGenerator
具有方法调用void Generate(...)
,该方法具有类型为IVsGeneratorProgress
的参数.该界面具有方法void GeneratorError()
,可用于将错误和警告报告给Visual Studio错误列表.
For adding items to the error list, the IVsSingleFileGenerator
has a method call void Generate(...)
which has a parameter of the type IVsGeneratorProgress
. This interface has a method void GeneratorError()
which lets you report errors and warnings to the Visual Studio error list.
public class MyCodeGenerator : IVsSingleFileGenerator
{
...
public void Generate( string inputFilePath, string inputFileContents, string defaultNamespace, out IntPtr outputFileContents, out int output, IVsGeneratorProgress generateProgress )
{
...
generateProgress.GeneratorError( false, 0, "An error occured", 2, 4);
...
}
...
}
GeneratorError()的详细信息可以在MSDN上找到.
The details of GeneratorError() can be found on MSDN.
这篇关于如何在“自定义工具"中写入Visual Studio输出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!