DesignerTransaction DesignerTransaction DesignerTransaction DesignerTransaction Class

定義

提供方式,用於群組一系列設計階段動作以改善效能,以及啟用大部分變更類型以復原。Provides a way to group a series of design-time actions to improve performance and enable most types of changes to be undone.

public ref class DesignerTransaction abstract : IDisposable
public abstract class DesignerTransaction : IDisposable
type DesignerTransaction = class
    interface IDisposable
Public MustInherit Class DesignerTransaction
Implements IDisposable
繼承
DesignerTransactionDesignerTransactionDesignerTransactionDesignerTransaction
實作

範例

下列程式碼範例程式會示範如何建立DesignerTransaction從設計工具。The following code example program demonstrates how to create a DesignerTransaction from a designer. 若要執行此範例,請將原始程式碼編譯至類別庫。To run this sample, compile the source code into a class library. 您必須新增 System.Design 組件的參考。You must add a reference to the System.Design assembly. 在新的專案中,將參考加入至已編譯的 DLL,並將元件新增至文件庫中工具箱In a new project, add a reference to the compiled DLL and add the component in the library to the Toolbox.

沒有這項功能在 Visual Studio 中的廣泛支援。There is extensive support for this feature in Visual Studio.

另請參閱逐步解說:自動將 [工具箱] 中的以自訂元件填入Also see Walkthrough: Automatically Populating the Toolbox with Custom Components.

設計工具可以選擇顯示設計工具異動事件的相關通知。The designer can optionally display notifications about designer transaction events. 如果您加入的執行個體DTComponent表單處於設計模式時,訊息方塊出現,詢問您想要接收設計工具異動事件通知。If you add an instance of the DTComponent to a form while in design mode, a message box appears asking whether you would like to receive designer transaction event notifications. 您可以切換使用捷徑功能表的執行個體上按一下滑鼠右鍵時出現這些告知DTComponentYou may toggle these notifications using the shortcut menu that appears when you right-click an instance of the DTComponent. 當您變更使用屬性視窗的值時,會建立交易。Transactions are created when you change values using the Properties window. 您也可以讓設計工具,即可執行異動執行範例交易元件的捷徑功能表上。You can also have the designer perform a transaction by clicking Perform Example Transaction on the shortcut menu for the component.

#using <system.dll>
#using <system.design.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;

/*
This sample demonstrates how to perform a series of actions in a designer
transaction, how to change values of properties of a component from a
designer, and how to complete transactions without being interrupted
by other activities.

To run this sample, add this code to a class library project and compile.
Create a new Windows Forms project or load a form in the designer. Add a
reference to the class library that was compiled in the first step.
Right-click the Toolbox in design mode and click Customize Toolbox.
Browse to the class library that was compiled in the first step and
select OK until the DTComponent item appears in the Toolbox.  Add an
instance of this component to the form.

When the component is created and added to the component tray for your
design project, the Initialize method of the designer is called.
This method displays a message box informing you that designer transaction
event handlers will be registered unless you click Cancel. When you set
properties in the properties window, each change will be encapsulated in
a designer transaction, allowing the change to be undone later.

When you right-click the component, the shortcut menu for the component
is displayed. The designer constructs this menu according to whether
designer transaction notifications are enabled, and offers the option
of enabling or disabling the notifications, depending on the current
mode. The shortcut menu also presents a Perform Example Transaction
item, which will set the values of the component's StringProperty and
CountProperty properties. You can undo the last designer transaction using
the Undo command provided by the Visual Studio development environment.
*/

private ref class DTDesigner: public ComponentDesigner
{
private:
   bool notification_mode;
   int count;
   void LinkDTNotifications( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      if (  !notification_mode )
      {
         IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
         if ( host != nullptr )
         {
            notification_mode = true;
            host->TransactionOpened += gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
            host->TransactionClosed += gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
         }
      }
   }

   void UnlinkDTNotifications( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      if ( notification_mode )
      {
         IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
         if ( host != nullptr )
         {
            notification_mode = false;
            host->TransactionOpened -= gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
            host->TransactionClosed -= gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
         }
      }
   }

   void OnDesignerTransactionOpened( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      MessageBox::Show( "A Designer Transaction was started. (TransactionOpened)" );
   }

   void OnDesignerTransactionClosed( Object^ /*sender*/, DesignerTransactionCloseEventArgs^ /*e*/ )
   {
      MessageBox::Show( "A Designer Transaction was completed. (TransactionClosed)" );
   }

   void DoTransaction( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      IDesignerHost^ host = static_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
      DesignerTransaction^ t = host->CreateTransaction( "Change Text and Size" );
      
      /* The code within the using statement is considered to be a single transaction.
              When the user selects Undo, the system will undo everything executed in this code block.
              */
      if ( notification_mode )
            MessageBox::Show( "Entering a Designer-Initiated Designer Transaction" );

      // The .NET Framework automatically associates the TypeDescriptor with the correct component
      PropertyDescriptor^ someText = TypeDescriptor::GetProperties( Component )[ "StringProperty" ];
      someText->SetValue( Component, "This text was set by the designer for this component." );
      PropertyDescriptor^ anInteger = TypeDescriptor::GetProperties( Component )[ "CountProperty" ];
      anInteger->SetValue( Component, count );
      count++;

      // Complete the designer transaction.
      t->Commit();
      if ( notification_mode )
            MessageBox::Show( "Designer-Initiated Designer Transaction Completed" );
   }

public:
   property DesignerVerbCollection^ Verbs 
   {
      // The Verbs property is overridden from ComponentDesigner
      virtual DesignerVerbCollection^ get() override
      {
         DesignerVerbCollection^ dvc = gcnew DesignerVerbCollection;
         dvc->Add( gcnew DesignerVerb( "Perform Example Transaction",gcnew EventHandler( this, &DTDesigner::DoTransaction ) ) );
         if ( notification_mode )
                  dvc->Add( gcnew DesignerVerb( "End Designer Transaction Notifications",
                     gcnew EventHandler( this, &DTDesigner::UnlinkDTNotifications ) ) );
         else
                  dvc->Add( gcnew DesignerVerb( "Show Designer Transaction Notifications",
                     gcnew EventHandler( this, &DTDesigner::LinkDTNotifications ) ) );

         return dvc;
      }
   }
   virtual void Initialize( IComponent^ component ) override
   {
      ComponentDesigner::Initialize( component );
      notification_mode = false;
      count = 10;
      IDesignerHost^ host = dynamic_cast<IDesignerHost^>(GetService( IDesignerHost::typeid ));
      if ( host == nullptr )
      {
         MessageBox::Show( "The IDesignerHost service interface could not be obtained." );
         return;
      }

      if ( MessageBox::Show( "Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.", "Link DesignerTransaction Notifications?", MessageBoxButtons::YesNo, MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, MessageBoxOptions::RightAlign ) == DialogResult::Yes )
      {
         host->TransactionOpened += gcnew EventHandler( this, &DTDesigner::OnDesignerTransactionOpened );
         host->TransactionClosed += gcnew DesignerTransactionCloseEventHandler( this, &DTDesigner::OnDesignerTransactionClosed );
         notification_mode = true;
      }
   }

public:
   ~DTDesigner()
   {
      UnlinkDTNotifications( this, gcnew EventArgs );
   }
};

// Associate the DTDesigner with this component

[DesignerAttribute(DTDesigner::typeid)]
public ref class DTComponent: public System::ComponentModel::Component
{
private:
   String^ m_String;
   int m_Count;
   void InitializeComponent()
   {
      m_String = "Initial Value";
      m_Count = 0;
   }

public:
   property String^ StringProperty 
   {
      String^ get()
      {
         return m_String;
      }

      void set( String^ value )
      {
         m_String = value;
      }
   }

   property int CountProperty 
   {
      int get()
      {
         return m_Count;
      }

      void set( int value )
      {
         m_Count = value;
      }
   }
};
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

/*
    This sample demonstrates how to perform a series of actions in a designer 
    transaction, how to change values of properties of a component from a 
    designer, and how to complete transactions without being interrupted 
    by other activities.

    To run this sample, add this code to a class library project and compile. 
    Create a new Windows Forms project or load a form in the designer. Add a 
    reference to the class library that was compiled in the first step.
    Right-click the Toolbox in design mode and click Customize Toolbox.  
    Browse to the class library that was compiled in the first step and 
    select OK until the DTComponent item appears in the Toolbox.  Add an 
    instance of this component to the form.  
	
    When the component is created and added to the component tray for your
    design project, the Initialize method of the designer is called. 
    This method displays a message box informing you that designer transaction
    event handlers will be registered unless you click Cancel. When you set 
    properties in the properties window, each change will be encapsulated in 
    a designer transaction, allowing the change to be undone later.  
	
    When you right-click the component,	the shortcut menu for the component 
    is displayed. The designer constructs this menu according to whether 
    designer transaction notifications are enabled, and offers the option
    of enabling or disabling the notifications, depending on the current 
    mode. The shortcut menu also presents a Perform Example Transaction 
    item, which will set the values of the component's StringProperty and 
    CountProperty properties. You can undo the last designer transaction using 
    the Undo command provided by the Visual Studio development environment.
*/

namespace DesignerTransactionSample
{
    // Associate the DTDesigner with this component
    [DesignerAttribute(typeof(DTDesigner))]
    public class DTComponent : System.ComponentModel.Component
    {
    	private string m_String;
	private int m_Count;
			
	public string StringProperty
	{
	    get
            { return m_String; }
	    set
	    { m_String = value; }
	}
			
	public int CountProperty
	{
	    get
	    { return m_Count; }
	    set
	    { m_Count = value; }
	}

	private void InitializeComponent()
	{
	    m_String = "Initial Value";
	    m_Count = 0;
	}
    }
	
    internal class DTDesigner : ComponentDesigner
    {
	private bool notification_mode = false;
	private int count = 10;
		
	// The Verbs property is overridden from ComponentDesigner
	public override DesignerVerbCollection Verbs
	{
	    get
	    {				
	        DesignerVerbCollection dvc = new DesignerVerbCollection();				
		dvc.Add( new DesignerVerb("Perform Example Transaction", new EventHandler(this.DoTransaction)) );
		if(notification_mode)
		    dvc.Add(new DesignerVerb("End Designer Transaction Notifications", new EventHandler(this.UnlinkDTNotifications)));
		else
		    dvc.Add(new DesignerVerb("Show Designer Transaction Notifications", new EventHandler(this.LinkDTNotifications)));				return dvc;
	    }
	}
		
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));			
            if(host == null)
            {
                MessageBox.Show("The IDesignerHost service interface could not be obtained.");
                return;
            }

            if( MessageBox.Show("Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.","Link DesignerTransaction Notifications?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign) == DialogResult.Yes )
            {							
	        host.TransactionOpened += new EventHandler(OnDesignerTransactionOpened);
    	        host.TransactionClosed += new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
                notification_mode = true;
            }
        }
		
        private void LinkDTNotifications(object sender, EventArgs e)
        {
            if(notification_mode == false)
            {
	        IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));							
                if(host != null)
	        {
		    notification_mode = true;
                   host.TransactionOpened += new EventHandler(OnDesignerTransactionOpened);
                   host.TransactionClosed += new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
	        }
	    }
        }

        private void UnlinkDTNotifications(object sender, EventArgs e)
        {
	    if(notification_mode)
    	    {
    	        IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));							
	        if(host != null)
                {				
		    notification_mode = false;
                    host.TransactionOpened -= new EventHandler(OnDesignerTransactionOpened);
                    host.TransactionClosed -= new DesignerTransactionCloseEventHandler(OnDesignerTransactionClosed);
                }
            }
        }

        private void OnDesignerTransactionOpened(object sender, EventArgs e)
        {			
	    System.Windows.Forms.MessageBox.Show("A Designer Transaction was started. (TransactionOpened)");
        }

        private void OnDesignerTransactionClosed(object sender, DesignerTransactionCloseEventArgs e)
        {			
	    System.Windows.Forms.MessageBox.Show("A Designer Transaction was completed. (TransactionClosed)");
        }   

        private void DoTransaction(object sender, EventArgs e) 
        {			
    	    IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));			
            DesignerTransaction t = host.CreateTransaction("Change Text and Size");

            /* The code within the using statement is considered to be a single transaction.
	       When the user selects Undo, the system will undo everything executed in this code block. */
            using (t)
            {
	        if(notification_mode)
	            System.Windows.Forms.MessageBox.Show("Entering a Designer-Initiated Designer Transaction");
				
                // The .NET Framework automatically associates the TypeDescriptor with the correct component
	        PropertyDescriptor someText = TypeDescriptor.GetProperties(Component)["StringProperty"];
                someText.SetValue(Component, "This text was set by the designer for this component.");

                PropertyDescriptor anInteger = TypeDescriptor.GetProperties(Component)["CountProperty"];
	        anInteger.SetValue(Component, count);
	        count++;

                // Complete the designer transaction.
	        t.Commit();
				
	        if(notification_mode)
	            System.Windows.Forms.MessageBox.Show("Designer-Initiated Designer Transaction Completed");
            }
        }
		
	protected override void Dispose(bool disposing)
	{
	    UnlinkDTNotifications(this, new EventArgs());
	    base.Dispose(disposing);
	}
    }
}
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

'   This sample demonstrates how to perform a series of actions in a designer 
'   transaction, how to change values of properties of a component from a 
'   designer, and how to complete transactions without being interrupted 
'   by other activities.

'   To run this sample, add this code to a class library project and compile. 
'   Create a new Windows Forms project or load a form in the designer. Add a 
'   reference to the class library that was compiled in the first step.
'   Right-click the Toolbox in design mode and click Customize Toolbox.  
'   Browse to the class library that was compiled in the first step and 
'   select OK until the DTComponent item appears in the Toolbox.  Add an 
'   instance of this component to the form.  

'   When the component is created and added to the component tray for your
'   design project, the Initialize method of the designer is called. 
'   This method displays a message box informing you that designer transaction
'   event handlers are being registered unless you click Cancel. When you set 
'   properties in the properties window, each change will be encapsulated in 
'   a designer transaction, allowing the change to be undone later.  

'   When you right-click the component, the shortcut menu for the component 
'   is displayed. The designer constructs this menu according to whether 
'   designer transaction notifications are enabled, and offers the option
'   of enabling or disabling the notifications, depending on the current 
'   mode. The shortcut menu also presents a Perform Example Transaction 
'   item which will set the values of the component's StringProperty and 
'   CountProperty properties. You can undo the last designer transaction using 
'   the Undo command provided by the Visual Studio development environment.	

Namespace DesignerTransactionSample

    ' Associate the DTDesigner with this component
    <DesignerAttribute(GetType(DTDesigner))> _
    Public Class DTComponent
        Inherits System.ComponentModel.Component
        Private m_String As String
        Private m_Count As Integer

        Public Property StringProperty() As String
            Get
                Return m_String
            End Get
            Set(ByVal Value As String)
                m_String = Value
            End Set
        End Property

        Public Property CountProperty() As Integer
            Get
                Return m_Count
            End Get
            Set(ByVal Value As Integer)
                m_Count = Value
            End Set
        End Property

        Private Sub InitializeComponent()
            m_String = "Initial Value"
            m_Count = 0
        End Sub 'InitializeComponent

    End Class 'DTComponent

    Friend Class DTDesigner
        Inherits ComponentDesigner

        Private notification_mode As Boolean = False
        Private count As Integer = 10

        ' The Verbs property is overridden from ComponentDesigner
        Public Overrides ReadOnly Property Verbs() As DesignerVerbCollection
            Get
                Dim dvc As New DesignerVerbCollection()
                dvc.Add(New DesignerVerb("Perform Example Transaction", AddressOf Me.DoTransaction))
                If notification_mode Then
                    dvc.Add(New DesignerVerb("End Designer Transaction Notifications", AddressOf Me.UnlinkDTNotifications))
                Else
                    dvc.Add(New DesignerVerb("Show Designer Transaction Notifications", AddressOf Me.LinkDTNotifications))
                End If
                Return dvc
            End Get
        End Property

        Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
            MyBase.Initialize(component)

            Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
            If host Is Nothing Then
                MessageBox.Show("The IDesignerHost service interface could not be obtained.")
                Return
            End If

            If MessageBox.Show("Press the Yes button to display notification message boxes for the designer transaction opened and closed notifications.", "Link DesignerTransaction Notifications?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign) = DialogResult.Yes Then
                AddHandler host.TransactionOpened, AddressOf OnDesignerTransactionOpened
                AddHandler host.TransactionClosed, AddressOf OnDesignerTransactionClosed
                notification_mode = True
            End If
        End Sub 'Initialize

        Private Sub LinkDTNotifications(ByVal sender As Object, ByVal e As EventArgs)
            If notification_mode = False Then
                Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
                If (host IsNot Nothing) Then
                    notification_mode = True
                    AddHandler host.TransactionOpened, AddressOf OnDesignerTransactionOpened
                    AddHandler host.TransactionClosed, AddressOf OnDesignerTransactionClosed
                End If
            End If
        End Sub 'LinkDTNotifications

        Private Sub UnlinkDTNotifications(ByVal sender As Object, ByVal e As EventArgs)
            If notification_mode Then
                Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
                If (host IsNot Nothing) Then
                    notification_mode = False
                    RemoveHandler host.TransactionOpened, AddressOf Me.OnDesignerTransactionOpened
                    RemoveHandler host.TransactionClosed, AddressOf Me.OnDesignerTransactionClosed
                End If
            End If
        End Sub 'UnlinkDTNotifications

        Private Sub OnDesignerTransactionOpened(ByVal sender As Object, ByVal e As EventArgs)
            System.Windows.Forms.MessageBox.Show("A Designer Transaction was started. (TransactionOpened)")
        End Sub 'OnDesignerTransactionOpened

        Private Sub OnDesignerTransactionClosed(ByVal sender As Object, ByVal e As DesignerTransactionCloseEventArgs)
            System.Windows.Forms.MessageBox.Show("A Designer Transaction was completed. (TransactionClosed)")
        End Sub 'OnDesignerTransactionClosed

        Private Sub DoTransaction(ByVal sender As Object, ByVal e As EventArgs)
            Dim host As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
            Dim t As DesignerTransaction = host.CreateTransaction("Change Text and Size")

            ' The code within the using statement is considered to be a single transaction.
            ' When the user selects Undo, the system will undo everything executed in this code block. 
            Try
                If (notification_mode) Then
                    System.Windows.Forms.MessageBox.Show("Entering a Designer-Initiated Designer Transaction")
                End If

                Dim someText As PropertyDescriptor = TypeDescriptor.GetProperties(Component)("StringProperty")
                someText.SetValue(Component, "This text was set by the designer for this component.")
                Dim anInteger As PropertyDescriptor = TypeDescriptor.GetProperties(Component)("CountProperty")
                anInteger.SetValue(Component, count)
                count = count + 1

                Exit Try
            Finally
                t.Commit()                
            End Try
            If (notification_mode) Then
                System.Windows.Forms.MessageBox.Show("Designer-Initiated Designer Transaction Completed")
            End If
        End Sub 'DoTransaction

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            UnlinkDTNotifications(Me, New EventArgs())
            MyBase.Dispose(disposing)
        End Sub 'Dispose

    End Class 'DTDesigner
End Namespace 'DesignerTransactionSample

備註

交易可以追蹤稍後可以復原的動作。Transactions can track actions that can be undone later. 藉由取消交易,這會自動嘗試將每個已變更的屬性設定為其變更前的值來反轉每項變更可以反轉交易期間所做的變更。Changes made during a transaction can be reversed by canceling a transaction, which automatically attempts to reverse each change by setting each changed property to its pre-change value. 交易也可以將一系列的作業期間的效能改善延後更新的顯示,直到交易完成。Transactions can also improve performance during a series of operations by deferring updates to the display until the completion of the transaction.

進行中交易時,某些元件延後其處理,直到交易完成藉由接聽TransactionOpeningTransactionClosed事件。When a transaction is in progress, some components defer their processing until the transaction has completed by listening to the TransactionOpening and TransactionClosed events. [屬性] 視窗中,比方說,不會更新它的顯示畫面之後的交易已開啟,直到交易已關閉。The Properties window, for example, does not update its display after a transaction has opened until the transaction has closed.

若要使用的交易復原或多個作業,已建立您的設計工具DesignerTransaction針對每個作業或系列的應該是可反轉的作業。To use transactions for reversible or multiple operations, have your designer create a DesignerTransaction for each operation or series of operations which should be reversible. 請小心,不要執行可能會阻止復原事件序列無法順利完成的交易之外的動作。Be careful not to perform actions outside the transactions that might prevent a sequence of undo events from completing successfully.

您可以取得新DesignerTransaction藉由呼叫CreateTransaction方法IDesignerHostYou can obtain a new DesignerTransaction by calling the CreateTransaction method of an IDesignerHost. 請務必取得每個DesignerTransaction從現用IDesignerHost才能正確地整合與處理機制,而不是建立新的設計工具異動DesignerTransaction直接。Be sure to obtain each DesignerTransaction from the active IDesignerHost in order to correctly integrate with the designer transaction processing mechanism, rather than creating a new DesignerTransaction directly.

若要執行交易內的某個動作,您必須先建立交易。To perform an action within a transaction, you must first create a transaction. 您必須呼叫OnComponentChanging方法之前發生每個變更或一組變更,而OnComponentChanged方法在每個變更或一組變更之後發生。Then you must call the OnComponentChanging method before each change or set of changes occurs, and the OnComponentChanged method after each change or set of changes occur. 最後,完成,然後藉由呼叫關閉交易Commit方法。Finally, complete and close the transaction by calling the Commit method.

注意

當屬性值進行變更,使用SetValue方法PropertyDescriptor,它會呼叫元件的變更方法IComponentChangeService,並建立DesignerTransaction自動代表變更。When making changes to property values, use the SetValue method of a PropertyDescriptor, which calls the component change methods of the IComponentChangeService and creates a DesignerTransaction representing the change automatically.

若要執行的交易,請完成下列步驟:To perform a transaction, complete the following steps:

  1. 呼叫CreateTransaction以取得DesignerTransaction,可用來控制交易。Call CreateTransaction to obtain a DesignerTransaction that can be used to control the transaction.

  2. try區塊中的,針對您想要追蹤與每個動作DesignerTransaction,呼叫OnComponentChanging方法,請變更或變更的項目,然後呼叫OnComponentChanged方法來通知有已進行變更。Within a try block, for each action that you want to track with a DesignerTransaction, call the OnComponentChanging method, make the change or changes, then call the OnComponentChanged method to signal that the change or changes have been made.

  3. 若要完成交易,呼叫Commit內在finally區塊。To complete the transaction, call Commit from within a finally block.

在 C# 中,您可以使用using陳述式而非try/finally封鎖,如下列範例所示。In C#, you can use the using statement rather than a try/finally block, such as in the following example.

using (host.CreateTransaction() {  
// Insert your code here.  
}  

若要取消並嘗試回復的交易已認可之前,請呼叫Cancel方法。To cancel and attempt to roll back a transaction before it has been committed, call the Cancel method. Cancel叫用方法時,所追蹤的動作DesignerTransaction顛倒,用以嘗試回復的變更。When the Cancel method is invoked, the actions tracked by the DesignerTransaction are reversed to attempt to roll back the changes. 若要復原的先前交易過程中發生的動作,您必須使用開發環境所提供的 [復原] 命令。To undo actions that occurred as part of earlier transactions, you must use the undo command provided by the development environment.

建構函式

DesignerTransaction() DesignerTransaction() DesignerTransaction() DesignerTransaction()

不使用描述,初始化 DesignerTransaction 類別的新執行個體。Initializes a new instance of the DesignerTransaction class with no description.

DesignerTransaction(String) DesignerTransaction(String) DesignerTransaction(String) DesignerTransaction(String)

使用指定的異動描述,初始化 DesignerTransaction 類別的新執行個體。Initializes a new instance of the DesignerTransaction class using the specified transaction description.

屬性

Canceled Canceled Canceled Canceled

取得值,表示異動是否已取消。Gets a value indicating whether the transaction was canceled.

Committed Committed Committed Committed

取得值,表示異動是否已認可。Gets a value indicating whether the transaction was committed.

Description Description Description Description

取得異動的描述。Gets a description for the transaction.

方法

Cancel() Cancel() Cancel() Cancel()

取消異動,並嘗試復原異動事件所做的變更。Cancels the transaction and attempts to roll back the changes made by the events of the transaction.

Commit() Commit() Commit() Commit()

認可這個異動。Commits this transaction.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

釋放 DesignerTransaction 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the DesignerTransaction and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

釋出與這個物件相關聯的資源。Releases the resources associated with this object. 如果這個異動還沒有認可,則這個覆寫會認可該異動。This override commits this transaction if it was not already committed.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnCancel() OnCancel() OnCancel() OnCancel()

引發 Cancel 事件。Raises the Cancel event.

OnCommit() OnCommit() OnCommit() OnCommit()

執行認可異動的實際工作。Performs the actual work of committing a transaction.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

釋放 DesignerTransaction 所使用的所有資源。Releases all resources used by the DesignerTransaction.

安全性

NamedPermissionSet
取得的完整存取系統資源。for full access to system resources. 要求值: InheritanceDemandDemand value: InheritanceDemand. 相關聯的狀態:Associated state:

適用於

另請參閱