本文介绍了如何从命令 Visual Studio Extension 2017 获取 IWpfTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 TextViewAdornment 来显示弹出窗口,它需要 IWpfTextView.有旧代码:

I need to show popup use TextViewAdornment, it's require IWpfTextView.There is old code to that:

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 Extension DefGuidList.guidIWpfTextViewHost 时丢失.所以我不能再获得 IWpfTextView 了.

but when go to Visual studio 2017 Extension DefGuidList.guidIWpfTextViewHost is missing. So I cannot get IWpfTextView anymore.

请帮帮我.谢谢大家.

推荐答案

在 Sergey Vlasov 回答之后我找到了一个解决方案:

After Sergey Vlasov answer I found a solution:

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);
}

您必须通过添加引用 -> 程序集 -> 扩展添加一些参考手册.然后选择:

You must add some reference manual by Add Reference -> Assemblies -> Extensions. Then choose:

 Microsoft.VisualStudio.ComponentModelHost 
 Microsoft.VisualStudio.Editor

这篇关于如何从命令 Visual Studio Extension 2017 获取 IWpfTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:46