共用方式為


調整傳統的程式碼,以新的編輯器

在編輯器Visual Studio 2010可提供許多功能,您可以從現有的程式碼元件存取。 下列指示會示範如何修改非 MEF 元件,例如,VSPackage,若要使用編輯器功能。 指示也會示範如何使用介面卡取得 managed 和 unmanaged 程式碼中的編輯器] 中的服務。

編輯器介面卡

編輯器介面卡或相容性修正減緩,是包裝函式也會公開的類別和介面中的編輯器物件Microsoft.VisualStudio.TextManager.Interop API。 您可以使用,例如移動非編輯器服務之間的介面卡、 Visual Studio 的殼層命令以及編輯器服務。 (這是編輯器] 中目前如何裝載於 Visual Studio)。 介面卡也啟用傳統編輯器和語言服務延伸模組能夠正確地在Visual Studio 2010。

使用編輯器介面卡

IVsEditorAdaptersFactoryService提供新的編輯器介面和繼承的介面之間切換的方法,以及建立配接器的方法。

如果您在 MEF 元件組件中使用這項服務,您可以以下列方式匯入服務。

[Import(typeof(IVsEditorAdaptersFactoryService))]
internal IVsEditorAdaptersFactoryService editorFactory;

如果您想要在非 MEF 元件中使用這項服務,請依照本主題稍後的 「 使用 Visual Studio 編輯器服務中非 MEF 元件 」 一節中的指示進行。

新的編輯器 API 與舊版 API 之間切換

編輯器物件和繼承的介面之間切換,請使用下列方法。

方法

轉換

GetBufferAdapter

ITextBuffer 轉換成 IVsTextBuffer

GetDataBuffer

IVsTextBuffer 轉換成 ITextBuffer

GetDocumentBuffer

IVsTextBuffer 轉換成 ITextBuffer

GetViewAdapter

ITextView 轉換成 IVsTextView

GetWpfTextView

IVsTextView 轉換成 IWpfTextView

建立配接器

使用下列方法來建立繼承的介面的配接器。

方法

轉換

CreateVsCodeWindowAdapter

建立 IVsCodeWindow

CreateVsTextBufferAdapter

會建立IVsTextBuffer針對指定IContentType

CreateVsTextBufferAdapter

建立 IVsTextBuffer

CreateVsTextBufferCoordinatorAdapter

建立 IVsTextBufferCoordinator

CreateVsTextViewAdapter

建立 ITextViewRoleSetIVsTextView

CreateVsTextViewAdapter

建立 IVsTextView

在 Unmanaged 程式碼中建立配接器

本機登錄所有的配接器類別 co-creatable,而且可以藉由具現化**VsLocalCreateInstance()**函式。

使用中的 vsshlids.h 檔案中所定義的 Guid 來建立所有的配接器。. Visual Studio 的 SDK 安裝的 \VisualStudioIntegration\Common\Inc\ 資料夾。 若要建立 VsTextBufferAdapter 的執行個體,請使用下列程式碼。

IVsTextBuffer *pBuf = NULL;
VsLocalCreateInstance(CLSID_VsTextBuffer, NULL, CLSCTX_INPROC_SERVER, IID_IVsTextBuffer, (void**)&pBuf);

在 Managed 程式碼中建立配接器

在 managed 程式碼,您可以在相同的方式所述的 unmanaged 程式碼中 co-create 介面卡。 您也可以使用 MEF 服務,可讓您建立並與其互動介面卡。 取得介面卡的這種方式可讓您更細微的控制已超過您當您在 co-create。

若要建立配接器的 IVsTextView

  1. 將參考加入至 Microsoft.VisualStudio.Editor.dll。 請確定CopyLocal設定為 [ false。

  2. 具現化IVsEditorAdaptersFactoryService、,如下所示。

    using Microsoft.VisualStudio.Editor;
    ...
    IVsEditorAdaptersFactoryService adapterFactoryService = ComponentModel.GetService<IVsEditorAdaptersFactoryService>();
    
  3. 呼叫 CreateX() 方法。

    adapterFactoryService.CreateTextViewAdapter(textView);
    

使用 Visual Studio 的編輯器,直接從 Unmanaged 程式碼

Microsoft.VisualStudio.Platform.VSEditor 命名空間和 Microsoft.VisualStudio.Platform.VSEditor.Interop 命名空間會將 COM 可呼叫的介面公開為 IVx * 介面。 例如,Microsoft.VisualStudio.Platform.VSEditor.Interop.IVxTextBuffer 介面是 COM 版本的ITextBuffer介面。 從IVxTextBuffer,您可以取得緩衝區快照集的存取、 修改緩衝區、 接聽文字變更事件的緩衝區,並建立追蹤點和跨。 下列步驟將示範如何存取IVxTextBufferIVsTextBuffer

若要取得 IVxTextBuffer

  1. IVx * 介面的定義是在 VSEditor.h 檔案中。. Visual Studio 的 SDK 安裝的 \VisualStudioIntegration\Common\Inc\ 資料夾。

  2. 下列程式碼具現化文字緩衝區使用IVsUserData->GetData()方法。 下列程式碼中, pData是指向IVsUserData物件。

    #include <textmgr.h>
    #include <VSEditor.h>
    #include <vsshlids.h>
    
    CComPtr<IVsTextBuffer> pVsTextBuffer;
    CComPtr<IVsUserData> pData;
    CComPtr<IVxTextBuffer> pVxBuffer;
    ...
    if (SUCCEEDED(pVsTextBuffer->QueryInterface(IID_IVsUserData, &pData))
    {
        CComVariant vt;
        if (SUCCEEDED(pData->GetData(GUID_VxTextBuffer, &vt)) &&
        (vt.Type == VT_UNKNOWN) && (vt.punkVal != NULL))
        {
            vt.punkVal->QueryInterface(IID_IVxTextBuffer, (void**)&pVxBuffer);
        }
    }
    

使用非 MEF 元件中的 Visual Studio 編輯器服務

如果您有現有的 managed 程式碼元件不需要使用 MEF,而您想要使用的 Visual Studio 編輯器服務,您必須加入包含 ComponentModelHost VSPackage 的組件的參考,並取得其 SComponentModel 的服務。

若要使用 Visual Studio 的編輯器元件,從非 MEF 元件

  1. 將參考加入至 Microsoft.VisualStudio.ComponentModelHost.dll 組件中。. \Common7\IDE\ Visual Studio 的安裝資料夾。 請確定CopyLocal設定為 [ false。

  2. 加入私用IComponentModel到您要以下列方式使用 Visual Studio 編輯器服務時,該類別的成員。

    using Microsoft.VisualStudio.ComponentModelHost;
    ....
    private IComponentModel componentModel;
    
  3. 具現化元件模型,在初始設定方法中,為您的元件。

    componentModel =
     (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
    
  4. 之後,您可以取得任何一種 Visual Studio 的編輯器服務藉由呼叫**IComponentModel.GetService<T>()**您所要之服務的方法。

    textBufferFactoryService =
         componentModel.GetService<ITextBufferFactoryService>();