2

I follow the steps in this page http://blogs.msdn.com/b/visualstudio/archive/2009/12/09/building-and-publishing-an-extension-for-visual-studio-2010.aspx

I create a TextAdornment project and a search box. I wan to do some different in this page. I want to query a link , get a list in the WPF user control and then write the info into the editor back. so the question is I do not know how to write the text back into the editor in seachbox(WPF user control)? I searched a lot, and get a way to use the code look like this:

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;
        }

However, the method "GetService" also said not found. I think the reason is this method is for VSPackage. and it is not suitable for Adornment project.

Please help to point how the write the text back into the editor from WPF user control. Thanks!

======================================================================================

Solution: when creating the SearchBox(WPF User Control), pass through the IWpfTextView to WPF control.and then,it is possible to use this in SearchBox.xaml.cs. Also need to be aware to use the Dispatcher function to keep the UI thread is the active one.

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();
        }));
4

1 に答える 1

2

あなたがパッケージに入っていて、どのビューが現在アクティブであるかを理解しようとしている場合、あなたが持っているコードはあります...それはあなたがやろうとしていることに対してやり過ぎです。

TextAdornmentテンプレートから開始したとすると、装飾オブジェクトにはコンストラクターでIWpfTextViewが与えられます。(そうでない場合は、おそらくIWpfTextCreationListener.TextViewCreatedそれを取得した実装があり、それをスレッド化する必要があります。)IWpfTextViewは、プロパティITextBufferを持つITextViewを派生させます。ここから、CreateEdit()を呼び出して、そこからテキストを編集できます。

于 2012-12-09T19:52:14.893 に答える