為設計工具提供復原支援Supply undo support to designers
設計工具(例如編輯器)通常需要支援復原作業,以便使用者在修改程式碼專案時,可以反轉其最近的變更。Designers, like editors, typically need to support undo operations so that users can reverse their recent changes when modifying a code element.
在 Visual Studio 中執行的大部分設計工具都有環境自動提供的「復原」支援。Most designers implemented in Visual Studio have "undo" support automatically provided by the environment.
需要提供復原功能支援的設計工具:Designer implementations that need to provide support for the undo feature:
藉由執行抽象基類來提供復原管理 UndoEngineProvide undo management by implementing the abstract base class UndoEngine
藉由執行和類別來提供持續性和 CodeDOM 支援 IDesignerSerializationService IComponentChangeService 。Supply persistence and CodeDOM support by implementing the IDesignerSerializationService and IComponentChangeService classes.
如需使用 .NET Framework 撰寫設計工具的詳細資訊,請參閱 擴充 Design-Time 支援。For more information on writing designers using .NET Framework, see Extend Design-Time Support.
Visual Studio SDKVisual Studio SDK提供預設的復原基礎結構,方式如下:The Visual Studio SDKVisual Studio SDK provides a default undo infrastructure by:
透過和類別提供復原管理 OleUndoEngine OleUndoEngine.UndoUnit 。Providing undo management implementations through the OleUndoEngine and OleUndoEngine.UndoUnit classes.
透過預設和實現提供持續性和 CodeDOM 支援 CodeDomComponentSerializationService IComponentChangeService 。Supplying persistence and CodeDOM support through the default CodeDomComponentSerializationService and IComponentChangeService implementations.
自動取得復原支援Obtain Undo Support Automatically
在 Visual Studio 中建立的任何設計工具,在設計工具時都具有自動和完整復原支援:Any designer created in Visual Studio has automatic and full undo support if, the designer:
使用型類別做 Control 為其使用者介面。Makes use of a Control based class for its user interface.
採用標準的 CodeDOM 型程式碼產生和剖析系統,以產生程式碼和持續性。Employs standard CodeDOM-based code generation and parsing system for code generation and persistence.
如需有關使用 Visual Studio CodeDOM 支援的詳細資訊,請參閱 動態原始程式碼產生和編譯。For more information about working with Visual Studio CodeDOM support, see Dynamic Source Code Generation and Compilation.
使用明確設計工具復原支援的時機When to Use Explicit Designer Undo Support
如果設計人員使用圖形化使用者介面(稱為 view adapter)(而非提供者),則必須提供自己的復原管理 Control 。Designers must supply their own undo management if they use a graphical user interface, referred to as a view adapter, other than the one supplied by Control.
其中一個範例可能是以 web 為基礎的圖形設計介面來建立產品,而不是以 .NET Framework 為基礎的圖形化介面。An example of this might be creating a product with a web-based graphical design interface rather than a .NET Framework-based graphical interface.
在這種情況下,您需要使用 Visual Studio 註冊此 view adapter ProvideViewAdapterAttribute ,並提供明確的復原管理。In such cases, one would need to register this view adapter with Visual Studio using ProvideViewAdapterAttribute, and provide explicit undo management.
如果設計工具未使用命名空間中提供的 Visual Studio 程式碼產生模型,則需要提供 CodeDOM 和持續性支援 System.CodeDom 。Designers need to provide CodeDOM and persistence support if they do not use the Visual Studio code generation model provided in the System.CodeDom name space.
復原設計工具的支援功能Undo Support Features of the Designer
環境 SDK 提供所需的預設介面介面,以提供復原支援,而設計工具可使用 Control 其使用者介面或標準 CodeDOM 和持續性模型,而不使用型類別。The Environment SDK provides default implementations of interfaces needed to provide undo support that can be used by designers not using Control based classes for their user interfaces or the standard CodeDOM and persistence model.
OleUndoEngine類別衍生自 .NET Framework 類別,其 UndoEngine 使用類別的實 IOleUndoManager 來管理復原作業。The OleUndoEngine class derives from the .NET Framework UndoEngine class using an implementation of the IOleUndoManager class to manage undo operations.
Visual Studio 提供下列功能給設計工具復原:Visual Studio provides the following feature to designer undo:
跨多個設計工具連結的復原功能。Linked undo functionality across multiple designers.
設計工具內的子單位可以藉由實和來與其父系互動 IOleUndoUnit IOleParentUndoUnit OleUndoEngine.UndoUnit 。Child units within a designer can interact with their parents by implementing IOleUndoUnit and IOleParentUndoUnit on OleUndoEngine.UndoUnit.
環境 SDK 提供 CodeDOM 和持續性支援,方法是提供:The Environment SDK provides CodeDOM and persistence support by supplying:
CodeDomComponentSerializationService 作為的實作為 IDesignerSerializationServiceCodeDomComponentSerializationService as an implementation of the IDesignerSerializationService
IComponentChangeService由 Visual Studio 設計主控制項提供。A IComponentChangeService provided by the Visual Studio design host.
使用環境 SDK 功能來提供復原支援Use the Environment SDK Features to Supply Undo Support
若要取得復原支援,執行設計工具的物件必須具現化,並 OleUndoEngine 使用有效的實初始化類別的實例 IServiceProvider 。To obtain undo support, an object implementing a designer must instantiate and initialize an instance of the OleUndoEngine class with a valid IServiceProvider implementation. 此 IServiceProvider 類別必須提供下列服務:This IServiceProvider class must provide the following services:
-
使用 Visual Studio CodeDOM 序列化的設計工具,可能會選擇使用 CodeDomComponentSerializationService 提供的 Visual Studio SDKVisual Studio SDK 做為的實作為 IDesignerSerializationService 。Designers using Visual Studio CodeDOM serialization may choose to use CodeDomComponentSerializationService provided with the Visual Studio SDKVisual Studio SDK as its implementation of the IDesignerSerializationService.
在此情況下, IServiceProvider 提供給此函 OleUndoEngine 式的類別應該會傳回這個物件做為類別的實作為 IDesignerSerializationService 。In this case, the IServiceProvider class provided to the OleUndoEngine constructor should return this object as an implementation of the IDesignerSerializationService class.
-
使用 DesignSurface Visual Studio design host 提供之預設值的設計工具保證具有類別的預設實值 IComponentChangeService 。Designers using the default DesignSurface provided by the Visual Studio design host are guaranteed to have a default implementation of the IComponentChangeService class.
執行以復原機制為基礎的設計工具 OleUndoEngine 會在下列情況自動追蹤變更:Designers implementing a OleUndoEngine based undo mechanism automatically tracks changes if:
屬性變更是透過物件進行 TypeDescriptor 。Property changes are made through the TypeDescriptor object.
IComponentChangeService 認可哥撤銷的變更時,會以手動方式產生事件。IComponentChangeService events are manually generated when an undoable change is committed.
設計工具上的修改是在的內容中建立的 DesignerTransaction 。Modification on the designer was created within the context of a DesignerTransaction.
設計工具選擇使用的執行所提供的標準復原單位 UndoEngine.UndoUnit 或衍生自的 Visual Studio 特定的實作為來明確建立復原單位 OleUndoEngine.UndoUnit ,而且 UndoEngine.UndoUnit 也提供和的實作為 IOleUndoUnit IOleParentUndoUnit 。The designer chooses to explicitly create undo units using either the standard undo unit provided by an implementation of UndoEngine.UndoUnit or the Visual Studio-specific implementation OleUndoEngine.UndoUnit, which derives from UndoEngine.UndoUnit and also provides an implementation of both IOleUndoUnit and IOleParentUndoUnit.