我想在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。
最佳答案
终于,我找到了解决方案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;
}
关于c# - 从 IWpfTextView 获取非 cs 文件的文档路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48068134/