如何使用MEF在Visual Studio Extension中获取当前ActiveDocument? [英] How to get Current ActiveDocument in Visual Studio Extension using MEF?

查看:109
本文介绍了如何使用MEF在Visual Studio Extension中获取当前ActiveDocument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试读取Active Document内容类型和代码时,我正在使用MEF开发Visual Studio 2013 Extension.目前,它仅在编辑器中Document/ProjectItem的打开时间读取.一旦打开它们,只要我们在打开的文档"选项卡之间切换,它就不会再次读取它们.

I'm working on Visual Studio 2013 Extension using MEF while trying to read Active Document Content Type and Code. Presently it only reads at Opening Time of the Document/ProjectItem in the Editor. Once these are opened, it doesn't read these again whenever we switch between opened Document Tabs.

要求::我希望此扩展程序读取当前Active Document的内容类型和代码文本.

Requirement: I want this extension to read the Content Type and Code Text of current Active Document.

已更新:
问题:我知道,使用EnvDTE80.DTE2.ActiveWindow可以获取当前关注的文档,但是我对此感到困惑,如何调用此代码来读取当前活动的文档/窗口"内容?假设有10个文档,则此扩展名需要读取活动文档(当前已成为焦点).在这里,VsTextViewCreated仅在我们打开一个新文档或在创建Text View之前关闭的文档时调用.不会在已经打开的文档(即已经创建的文本视图)上调用它,因此在将焦点移到其他已经打开的文档上时,我们将无法获取更新的wpfTextView对象.而且我在这里很困惑如何使用DTE2.ActiveDocument或DTE2.ActiveWindow事件处理程序来调用它.

Updated:
Problem: I know, using EnvDTE80.DTE2.ActiveWindow, I can get currently focused document, but I'm confused here that how to call this code to read the Currently Active Document/Window things? Let's say if we have 10 Documents, the active document (which got current focus) needs to be read by the this extension. And here VsTextViewCreated is only called whenever we open a new document or the one closed before i.e Text View is Created. It won't be called upon already open documents (i.e. Text View already created) and so we won't be able to get updated wpfTextView object upon moving the focus on other already open documents. And I'm confused here how to call this using DTE2.ActiveDocument or DTE2.ActiveWindow event handlers.

问题:
不使用DTE就可以在MEF中实现吗?
VS编辑器中是否已经存在处理TextView的接口?

Question:
Is this possible in MEF, without using DTE?
Is there any Interface dealing with TextViews already present in VS editor?

这是我的代码:

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading.Tasks;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Text.Editor;

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using System.Diagnostics;

namespace VSIXProject_Test
{
    [Export(typeof(IVsTextViewCreationListener))]
    [ContentType("code")]
    [TextViewRole(PredefinedTextViewRoles.Editable)]
    class VsTextViewCreationListener : IVsTextViewCreationListener
    {
        [Import]
        IVsEditorAdaptersFactoryService AdaptersFactory = null;

        public void VsTextViewCreated(IVsTextView textViewAdapter)
        {
            var wpfTextView = AdaptersFactory.GetWpfTextView(textViewAdapter);
            if (wpfTextView == null)
            {
                Debug.Fail("Unable to get IWpfTextView from text view adapter");
                return;
            }

            Debug.Write(wpfTextView.TextBuffer.ContentType.TypeName);
        }
    }
}

推荐答案

幸运的是,我得到了我想要实现的目标. 此处已发布了一个帮助程序解决方案: 我在dte2.Events.WindowsEvents.WindowActived中使用了辅助方法,并获取IVsTextView对象来检索文本缓冲区.这是我的WindowActivated事件的代码段:

Fortunately, I got what I was trying to achieve. A helper solution is already posted here: I used the helper method in dte2.Events.WindowsEvents.WindowActived and getting IVsTextView object to retrieve the Text Buffer. Here is my Code snippet of WindowActivated event:

void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)
    {

        if (null != GotFocus.Document)
        {
            Document curDoc = GotFocus.Document;
            Debug.Write("Activated : " + curDoc.FullName);
            this.IvsTextView=GetIVsTextView(curDoc.FullName); //Calling the helper method to retrieve IVsTextView object.
            if (IvsTextView != null)
            {
                IvsTextView.GetBuffer(out curDocTextLines); //Getting Current Text Lines 

                //Getting Buffer Adapter to get ITextBuffer which holds the current Snapshots as wel..
                Microsoft.VisualStudio.Text.ITextBuffer curDocTextBuffer = AdaptersFactory.GetDocumentBuffer(curDocTextLines as IVsTextBuffer); 
                Debug.Write("\r\nContentType: "+curDocTextBuffer.ContentType.TypeName+"\nTest: " + curDocTextBuffer.CurrentSnapshot.GetText());

            }
        }
    }

这现在可以处理在VS编辑器中打开的所有代码文档.希望这会对像我这样的人有所帮助.

This is now working with all the code documents opened in VS Editor. Hope this would help others like me.

这篇关于如何使用MEF在Visual Studio Extension中获取当前ActiveDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆