问题
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