问题描述
我正在开发我的第一个Visual Studio(2015社区)命令菜单,并且试图访问IEditorOperations
来删除文本,发送退格键等.但是我不确定该怎么做.我可以做到:
I'm developing my first Visual Studio (2015 Community) Command Menu and I'm trying to get access to IEditorOperations
to delete text, send backspace etc. but I'm not sure how to. I can do:
var Service = Provider.GetService(typeof(IEditorOperationsFactoryService)) as IEditorOperationsFactoryService;
Service.GetEditorOperations(???);
我不确定要在???
中传递什么,因为我没有访问ITextView
的权限,而是通过以下方式获得了IVsTExtView
的权限:
I'm not sure what to pass in the ???
since I don't have access to an ITextView
instead what I have is a IVsTExtView
via:
IVsTextView View;
IVsTextManager Manager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
int MustHaveFocus = 1;
Manager.GetActiveView(MustHaveFocus, null, out View);
在创建命令菜单"时,VS会使用私有ctor为我生成一个模板,该ctor创建命令服务,并将其绑定到命令集ID等.覆盖的Initialize
方法和一堆属性.
When creating the Command Menu, VS generates a template for me with a private ctor creating the command service, binding it to the command set id etc. An overridden Initialize
method, and a bunch of properties.
有什么想法吗?
编辑:在谢尔盖(Sergey)的帮助下,我设法进一步走了一步.但是现在当我尝试获取IEditorOperationsFactoryService
时,我得到一个空值,所有其他值都有效.
After help from Sergey, I managed to get a bit further. But now I get a null when I try to get the IEditorOperationsFactoryService
, all the other values are valid.
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);
}
推荐答案
您可以从名为Model的变量获取IEditorOperationsFactoryService实例,如下所示:
You can get IEditorOperationsFactoryService instance from variable named Model, like this:
var Model = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Model.GetService<IEditorOperationsFactoryService>();
这篇关于如何从IVsTextView获取IEditorOperations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!