如何从 IVsTextView 获取 IEditorOperations?
How to get IEditorOperations from IVsTextView?
我正在开发我的第一个 Visual Studio(2015 社区)命令菜单,我正在尝试访问 IEditorOperations
以删除文本、发送退格键等,但我不确定如何。我能做到:
var Service = Provider.GetService(typeof(IEditorOperationsFactoryService)) as IEditorOperationsFactoryService;
Service.GetEditorOperations(???);
我不确定要在 ???
中传递什么,因为我无法访问 ITextView
而我拥有的是 IVsTExtView
通过:
IVsTextView View;
IVsTextManager Manager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
int MustHaveFocus = 1;
Manager.GetActiveView(MustHaveFocus, null, out View);
创建命令菜单时,VS 为我生成了一个模板,其中包含创建命令服务的私有构造函数,将其绑定到命令集 ID 等。一个重写的 Initialize
方法和一堆属性。
有什么想法吗?
编辑: 在 Sergey 的帮助下,我取得了一些进展。但是现在当我尝试获取 IEditorOperationsFactoryService
时得到一个空值,所有其他值都有效。
static IEditorOperations GetEditorService(IServiceProvider Provider, IVsTextView VsView)
{
IEditorOperations Result;
try
{
var Model = (IComponentModel)Provider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Provider.GetService(typeof(IEditorOperationsFactoryService)); // returns null
var Adaptor = Model.GetService<IVsEditorAdaptersFactoryService>();
IWpfTextView TextView = Adaptor.GetWpfTextView(VsView);
Result = Editor.GetEditorOperations(TextView);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
Result = null;
}
return (Result);
}
您可以使用以下方法从 IVsTextView 获取 IWpfTextView(实现 ITextView):
IVsTextView textView = ...;
IWpfTextView v = GetEditorAdaptersFactoryService().GetWpfTextView(textView);
private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =
(Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(
typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
}
您可以从名为 Model 的变量中获取 IEditorOperationsFactoryService 实例,如下所示:
var Model = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Model.GetService<IEditorOperationsFactoryService>();
我正在开发我的第一个 Visual Studio(2015 社区)命令菜单,我正在尝试访问 IEditorOperations
以删除文本、发送退格键等,但我不确定如何。我能做到:
var Service = Provider.GetService(typeof(IEditorOperationsFactoryService)) as IEditorOperationsFactoryService;
Service.GetEditorOperations(???);
我不确定要在 ???
中传递什么,因为我无法访问 ITextView
而我拥有的是 IVsTExtView
通过:
IVsTextView View;
IVsTextManager Manager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
int MustHaveFocus = 1;
Manager.GetActiveView(MustHaveFocus, null, out View);
创建命令菜单时,VS 为我生成了一个模板,其中包含创建命令服务的私有构造函数,将其绑定到命令集 ID 等。一个重写的 Initialize
方法和一堆属性。
有什么想法吗?
编辑: 在 Sergey 的帮助下,我取得了一些进展。但是现在当我尝试获取 IEditorOperationsFactoryService
时得到一个空值,所有其他值都有效。
static IEditorOperations GetEditorService(IServiceProvider Provider, IVsTextView VsView)
{
IEditorOperations Result;
try
{
var Model = (IComponentModel)Provider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Provider.GetService(typeof(IEditorOperationsFactoryService)); // returns null
var Adaptor = Model.GetService<IVsEditorAdaptersFactoryService>();
IWpfTextView TextView = Adaptor.GetWpfTextView(VsView);
Result = Editor.GetEditorOperations(TextView);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
Result = null;
}
return (Result);
}
您可以使用以下方法从 IVsTextView 获取 IWpfTextView(实现 ITextView):
IVsTextView textView = ...;
IWpfTextView v = GetEditorAdaptersFactoryService().GetWpfTextView(textView);
private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =
(Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(
typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
}
您可以从名为 Model 的变量中获取 IEditorOperationsFactoryService 实例,如下所示:
var Model = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Model.GetService<IEditorOperationsFactoryService>();