共用方式為


UndoContext 介面

以單一的交易來表示針對 Visual Studio 中所有參與之開啟文件執行的所有作業。 如果叫用其 SetAborted 方法,所有在開啟物件後進行的變更都會被捨棄。

命名空間:  EnvDTE
組件:  EnvDTE (在 EnvDTE.dll 中)

語法

'宣告
<GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")> _
Public Interface UndoContext
[GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")]
public interface UndoContext
[GuidAttribute(L"D8DEC44D-CAF2-4B39-A539-B91AE921BA92")]
public interface class UndoContext
[<GuidAttribute("D8DEC44D-CAF2-4B39-A539-B91AE921BA92")>]
type UndoContext =  interface end
public interface UndoContext

UndoContext 類型會公開下列成員。

屬性

  名稱 描述
公用屬性 DTE 取得最上層的擴充性物件。
公用屬性 IsAborted 取得 UndoContext 物件操作是否為 SetAborted 方法所終止。
公用屬性 IsOpen 取得復原作業目前是否生效,或者是否開啟了方案。
公用屬性 IsStrict 取得復原堆疊連結性是否嚴格。
公用屬性 Parent 取得 UndoContext 物件的直接上層父物件。

回頁首

方法

  名稱 描述
公用方法 Close 結束復原作業。
公用方法 Open 開始新復原作業。
公用方法 SetAborted 捨棄自開啟 UndoContext 物件後所有針對參與開啟文件進行的變更。

回頁首

備註

UndoContext 物件會由 _DTE 物件的 UndoContext 屬性傳回。 Visual Studio 中只有一個全域復原服務,不是開啟就是關閉。 在 UndoContext 物件被開啟後,所有針對 Visual Studio 中文件所進行的更新,都可以藉由單一的復原動作,SetAborted 方法來回復 (復原),直到該物件關閉為止。 如果 SetAborted 方法被叫用,所有在開啟物件後進行的變更都會被捨棄。

若要使用 UndoContext 物件,請先開啟它,接著進行文件變更,然後關閉物件來完成變更。

若要捨棄變更,請在關閉 UndoContext 物件之前叫用 SetAborted 方法。 在您關閉 UndoContext 物件後,便無法復原變更。

注意事項注意事項

在使用 UndoContext 物件前,請先檢查它是否已在先前的作業中開啟。如果它已經開啟,則您在所有參與文件中的變更將被復原,而且物件首次開啟後執行的所有變更都會被復原。如果在您核取 UndoContext 物件時它已經開啟,請勿將之關閉,因為前一個呼叫端仍需要它來執行復原作業。因此,除非您是原始的開啟者,否則不應呼叫 SetAborted,然後關閉 UndoContext 物件。在您完成於文件中變更文字後,請將之關閉。

範例

Sub UndoContextExample()
   ' Before running, select text in an open document.
   Dim txtSel As TextSelection
   Dim strTS As String, boolWasOpen As Boolean
   txtSel = DTE.ActiveDocument.Selection
   strTS = txtSel.Text

   ' Check to see if UndoContext object is already open.
   If DTE.UndoContext.IsOpen = True Then
      boolWasOpen = True
   Else
      ' Open the UndoContext object to track changes.
      DTE.UndoContext.Open("RemoveNewLines", False)
   End If

   ' Perform search for newline characters and remove them.
   If strTS <> "" Then
      txtSel.Delete()
      strTS = Replace(strTS, vbNewLine, "", Compare:=vbTextCompare)
      txtSel.Insert(strTS)
   End If

   ' If UndoContext was already open, do not close it.
   If boolWasOpen = False Then
      ' Close the UndoContext object to commit the changes.
      DTE.UndoContext.Close()
   End If
End Sub

請參閱

參考

EnvDTE 命名空間