How to get IWpfTextView from command Visual Studio Extension 2017
Asked Answered
P

1

3

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.

Passing answered 18/8, 2017 at 8:20 Comment(4)
See https://mcmap.net/q/912496/-how-to-get-ieditoroperations-from-ivstextviewBrackely
Thank you so much!Passing
For other people: you must add reference manual. Add Reference -> Assemblies -> Extensions then choose Microsoft.VisualStudio.ComponentModelHost and Microsoft.VisualStudio.EditorPassing
@Tan Nguyen, So this issue has been resolved, am I right? If so, would you please post the solution as an answer? So you could mark it as the answer later and help other community members who get the same issue as yours.Elfin
P
5

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
Passing answered 22/8, 2017 at 17:34 Comment(1)
Thanks for sharing your solution here, please mark it as the answer, have a nice day:)Elfin

© 2022 - 2024 — McMap. All rights reserved.