我创建了一个 TextAdornment 项目和一个搜索框。我想在这个页面做一些不同的事情。我想查询一个链接,在 WPF 用户控件中获取一个列表,然后将信息写回到编辑器中。所以问题是我不知道如何将文本写回seachbox(WPF用户控件)中的编辑器? 我搜索了很多,得到了一种使用代码的方法,如下所示:
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
IVsTextView vTextView = null;
int mustHaveFocus = 1;
txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
IVsUserData userData = vTextView as IVsUserData;
if (userData == null)
{
return null;
}
else
{
IWpfTextViewHost viewHost;
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;
return viewHost;
}
但是“GetService”方法也说找不到。我认为原因是这个方法是针对VSPackage的。并且它不适合装饰项目。
请帮助指出如何从 WPF 用户控件将文本写回编辑器。谢谢!
================================================== =========================================
解决方案: 创建SearchBox(WPF用户控件)时,通过IWpfTextView传递给WPF控件。然后,可以在SearchBox.xaml.cs中使用它。还需要注意使用 Dispatcher 函数来保持 UI 线程处于事件状态。
Dispatcher.Invoke(new Action(() =>
{
ITextEdit edit = _view.TextBuffer.CreateEdit();
ITextSnapshot snapshot = edit.Snapshot;
int position = snapshot.GetText().IndexOf("gist:");
edit.Delete(position, 5);
edit.Insert(position, "billmo");
edit.Apply();
}));
最佳答案
您那里的代码是,如果您在一个包中并且您正在尝试找出当前事件的 View ......这对于您想要做的事情来说是多余的。
假设您从 TextAdornment 模板开始,装饰对象在构造函数中被赋予一个 IWpfTextView。 (如果没有,您可能有一个 IWpfTextCreationListener.TextViewCreated 的实现,它得到了它,并且您需要将其线程化。) IWpfTextView 派生 ITextView,它具有 ITextBuffer 属性。从这里,您可以调用 CreateEdit() 并从那里编辑文本。
关于c# - 如何在 Visual Studio 中的 TextAdornment 模板的编辑器中插入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788221/