我需要使用 TextViewAdornment 显示弹出窗口,它需要 IWpfTextView。 有旧代码:
private IWpfTextView GetWpfTextView(IVsTextView vTextView)
{
IWpfTextView view = null;
IVsUserData userData = vTextView as IVsUserData;
if (null != userData)
{
IWpfTextViewHost viewHost;
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;
view = viewHost.TextView;
}
return view;
}
但是当转到 Visual studio 2017 时,扩展 DefGuidList.guidIWpfTextViewHost 丢失。所以我无法再获取 IWpfTextView 了。
请帮助我。 谢谢大家。
最佳答案
Sergey Vlasov 回答后我找到了解决方案:
private IWpfTextView GetWpfView()
{
var textManager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
var componentModel = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
var editor = componentModel.GetService<IVsEditorAdaptersFactoryService>();
textManager.GetActiveView(1, null, out IVsTextView textViewCurrent);
return editor.GetWpfTextView(textViewCurrent);
}
您必须通过添加引用 -> 程序集 -> 扩展来添加一些引用手册。然后选择:
Microsoft.VisualStudio.ComponentModelHost
Microsoft.VisualStudio.Editor
关于c# - 如何从命令 Visual Studio Extension 2017 获取 IWpfTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45751908/