我想在VS中的文本编辑器中添加一些按钮。在创建ViewPort管理器的ViewPortTextViewCreationListener中,我想知道文档的路径或它
public void TextViewCreated(IWpfTextView textView) {
var result = _textDocumentFactoryService.TryGetTextDocument(TextView.TextBuffer, out ITextDocument textDocument);
new ViewPortSwitcher(textView);
}
我尝试使用ITextDocumentFactoryService
从TextBuffer获取ITextDocument
(请参阅回答here)。如果我打开cs文件它可以正常工作。但是如果我打开cshtml文件,TryGetTextDocument会返回false。
答案 0 :(得分:2)
最后,我找到了解决方案see MSDN forum:
public static string GetPath(this IWpfTextView textView) {
textView.TextBuffer.Properties.TryGetProperty(typeof(IVsTextBuffer), out IVsTextBuffer bufferAdapter);
var persistFileFormat = bufferAdapter as IPersistFileFormat;
if (persistFileFormat == null) {
return null;
}
persistFileFormat.GetCurFile(out string filePath, out _);
return filePath;
}