編輯器 Factory
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
編輯器 factory 建立編輯器物件,並將它們放在視窗框架中稱為實體的檢視。 它會建立文件資料及建立編輯器和設計工具所需的文件檢視物件。 編輯器 factory,才能建立 Visual Studio 核心編輯器和任何標準的編輯器。 編輯器 factory 可以選擇性地建立自訂編輯器。
您可以建立編輯器 factory 的實作IVsEditorFactory介面。 下列範例說明如何實作IVsEditorFactory建立編輯器 factory:
[Guid(GuidList.guidEditorFactory)]
public sealed class SingleViewEditorFactory : IVsEditorFactory, IDisposable
{
private PackageSingleViewEditor MyPackage;
private ServiceProvider vsServiceProvider;
public SingleViewEditorFactory(PackageSingleViewEditor packageEditor)
{
Trace.WriteLine(string.Format(CultureInfo.CurrentCulture,
"Entering {0} constructor", this.ToString()));
MyPackage = packageEditor;
}
#region "IVsEditorFactory Support"
public int Close()
{
throw new NotImplementedException();
}
public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out System.Guid pguidCmdUI, out int pgrfCDW)
{
throw new NotImplementedException();
}
public int MapLogicalView(ref System.Guid rguidLogicalView, out string pbstrPhysicalView)
{
throw new NotImplementedException();
}
public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
{
throw new NotImplementedException();
}
#endregion
#region "IDisposable Support"
public void Dispose()
{
throw new NotImplementedException();
}
#endregion
}
<Guid(GuidList.guidEditorFactory)> _
Public NotInheritable Class SingleViewEditorFactory
Implements IVsEditorFactory
Implements IDisposable
Private MyPackage As PackageSingleViewEditor
Private vsServiceProvider As ServiceProvider
Public Sub New(ByVal packageEditor As PackageSingleViewEditor)
Trace.WriteLine(String.Format(CultureInfo.CurrentCulture,
"Entering {0} constructor", Me.ToString()))
MyPackage = packageEditor
End Sub
#Region "IVsEditorFactorySupport"
Public Function Close() As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.Close
Throw New NotImplementedException
End Function
Public Function CreateEditorInstance(ByVal grfCreateDoc As UInteger, ByVal pszMkDocument As String, ByVal pszPhysicalView As String, ByVal pvHier As VisualStudio.Shell.Interop.IVsHierarchy, ByVal itemid As UInteger, ByVal punkDocDataExisting As System.IntPtr, ByRef ppunkDocView As System.IntPtr, ByRef ppunkDocData As System.IntPtr, ByRef pbstrEditorCaption As String, ByRef pguidCmdUI As System.Guid, ByRef pgrfCDW As Integer) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.CreateEditorInstance
Throw New NotImplementedException
End Function
Public Function MapLogicalView(ByRef rguidLogicalView As System.Guid, ByRef pbstrPhysicalView As String) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.MapLogicalView
Throw New NotImplementedException
End Function
Public Function SetSite(ByVal psp As VisualStudio.OLE.Interop.IServiceProvider) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.SetSite
Throw New NotImplementedException
End Function
#End Region
#Region "IDisposable Support"
Public Sub Dispose() Implements IDisposable.Dispose
Throw New NotImplementedException
End Sub
#End Region
End Class
編輯器會開啟該編輯器所處理的檔案類型的第一次載入。 您可以選擇特定的編輯器或預設的編輯器開啟。 如果您選取的預設編輯器,整合式的開發環境 (IDE) 會決定正確的編輯器開啟,並接著會開啟。 如需詳細資訊,請參閱判斷哪一個編輯器在專案中開啟檔案。
登錄編輯器 Factory
您可以使用您已建立編輯器之前,您必須先登錄其相關資訊,包括它可以處理的副檔名。
如果您 VSPackage 撰寫 managed 程式碼中,您可以使用管理套件架構 (MPF) 方法RegisterEditorFactory登錄編輯器 factory 之後載入 VSPackage。 如果您 VSPackage 撰寫的 unmanaged 程式碼,則您必須使用登錄編輯器 factory SVsRegisterEditors服務。
使用登錄編輯器 Factory Managed 程式碼
您必須在您的 VSPackage 中登錄編輯器 factoryInitialize
方法。 第一次呼叫base.Initialize
,然後呼叫RegisterEditorFactory每個編輯器 factory
在 managed 程式碼,沒有取消登錄編輯器 factory,需要因為 VSPackage 會處理這項工作。 此外,如果您的編輯器 factory 實作IDisposable,自動處置時取消註冊。
註冊使用 unmanaged 程式碼編輯器 factory
在SetSite實作您編輯器封裝時,使用QueryService
方法以呼叫SVsRegisterEditors
。 如此一來,傳回的指標IVsRegisterEditors。 呼叫RegisterEditor方法傳遞的實作IVsEditorFactory介面。 您必須 mplement IVsEditorFactory在個別的類別。
編輯器 Factory 註冊程序
Visual Studio 會載入您使用編輯器 factory 的編輯器時,就會發生下列處理程序︰
Visual Studio專案系統呼叫OpenStandardEditor。
這個方法會傳回編輯器 factory。 Visual Studio 延遲載入編輯器的封裝,不過,直到實際需要的專案系統的編輯器。
當專案系統需求編輯器] 中時,Visual Studio 會呼叫CreateEditorInstance,特定的方法會傳回文件檢視和文件資料物件。
如果 Visual Studio 編輯器使用 factory 呼叫CreateEditorInstance傳回文件資料物件和文件檢視物件時,Visual Studio 然後會建立文件視窗中,置於文件檢視物件可執行文件中的表格 (RDT) 的文件資料的物件將項目。