我按照此頁面中的步驟http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx如何在Visual Studio的TextAdornment模板中的編輯器中插入文本?
我創建了一個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用戶控件將文本寫回編輯器。謝謝!
============================================== ========================================
解決方案: when創建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();
}));
感謝您的回答。但我想我需要澄清我的問題。在創建TextAdornment模板之後,確實有兩個類「TextAdornment.cs」和「TextAdornmentFactory.cs」。然後,按照我的問題中的鏈接,我在此項目中添加一個WPF用戶控件。我的問題是,我想從這個WPF用戶控件中觸發一個操作(在編輯器中添加一些文本),我認爲我的代碼應該寫入此WPF用戶控件的類中。所以這是問題。我如何從這個WPF控件將文本添加到編輯器中。 – Ling
您的意思是您想要在編輯器中獲取文本並將其複製到WPF控件,或者您希望WPF控件能夠將文本插入到編輯器中? –
我希望WPF控件能夠將文本插入編輯器 – Ling