How to get IWpfTextView from command Visual Studio Extension 2017

半腔热情 提交于 2021-02-18 18:14:24

问题


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

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

Please help me. Thank you everyone.


回答1:


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


来源:https://stackoverflow.com/questions/45751908/how-to-get-iwpftextview-from-command-visual-studio-extension-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!