Automation.AddStructureChangedEventHandler 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
註冊要處理結構變更之事件的方法。
public:
static void AddStructureChangedEventHandler(System::Windows::Automation::AutomationElement ^ element, System::Windows::Automation::TreeScope scope, System::Windows::Automation::StructureChangedEventHandler ^ eventHandler);
public static void AddStructureChangedEventHandler (System.Windows.Automation.AutomationElement element, System.Windows.Automation.TreeScope scope, System.Windows.Automation.StructureChangedEventHandler eventHandler);
static member AddStructureChangedEventHandler : System.Windows.Automation.AutomationElement * System.Windows.Automation.TreeScope * System.Windows.Automation.StructureChangedEventHandler -> unit
Public Shared Sub AddStructureChangedEventHandler (element As AutomationElement, scope As TreeScope, eventHandler As StructureChangedEventHandler)
參數
- element
- AutomationElement
要與事件處理常式產生關聯的消費者介面自動化專案。
- scope
- TreeScope
要處理的事件範圍,意即,事件是項目本身或其項目的祖系及子系。
- eventHandler
- StructureChangedEventHandler
結構變更事件發生時要呼叫的方法。
範例
下列範例顯示每當指定 AutomationElement 之子樹狀結構變更時,都會呼叫的結構變更事件處理常式委派。
/// <summary>
/// Handles structure-changed events. If a new app window has been added, this method ensures
/// it's in the list of runtime IDs and subscribed to window-close events.
/// </summary>
/// <param name="sender">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
/// <remarks>
/// An exception can be thrown by the UI Automation core if the element disappears
/// before it can be processed -- for example, if a menu item is only briefly visible.
/// This exception cannot be caught here because it crosses native/managed boundaries.
/// In the debugger, you can ignore it and continue execution. The exception does not cause
/// a break when the executable is being run.
/// </remarks>
private void OnStructureChanged(object sender, StructureChangedEventArgs e)
{
AutomationElement element = sender as AutomationElement;
if (e.StructureChangeType == StructureChangeType.ChildAdded)
{
Object windowPattern;
if (false == element.TryGetCurrentPattern(WindowPattern.Pattern, out windowPattern))
{
return;
}
int[] rid = e.GetRuntimeId();
if (RuntimeIdListed(rid, savedRuntimeIds) < 0)
{
AddToWindowHandler(element);
savedRuntimeIds.Add(rid);
}
}
}
''' <summary>
''' Handles structure-changed events. If a new app window has been added, this method ensures
''' it's in the list of runtime IDs and subscribed to window-close events.
''' </summary>
''' <param name="sender">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
''' <remarks>
''' An exception can be thrown by the UI Automation core if the element disappears
''' before it can be processed -- for example, if a menu item is only briefly visible.
''' This exception cannot be caught here because it crosses native/managed boundaries.
''' In the debugger, you can ignore it and continue execution. The exception does not cause
''' a break when the executable is being run.
''' </remarks>
Private Sub OnStructureChanged(ByVal sender As Object, ByVal e As StructureChangedEventArgs)
Dim element As AutomationElement = DirectCast(sender, AutomationElement)
If e.StructureChangeType = StructureChangeType.ChildAdded Then
Dim myWindowPattern As Object = Nothing
If False = element.TryGetCurrentPattern(WindowPattern.Pattern, myWindowPattern) Then
Return
End If
Dim rid As Integer() = e.GetRuntimeId()
If RuntimeIdListed(rid, savedRuntimeIds) < 0 Then
AddToWindowHandler(element)
savedRuntimeIds.Add(rid)
End If
End If
End Sub
下列範例程式碼會新增委派的實例。
// elementRoot is an AutomationElement.
Automation.AddStructureChangedEventHandler(elementRoot, TreeScope.Children,
new StructureChangedEventHandler(OnStructureChanged));
' elementRoot is an AutomationElement.
Automation.AddStructureChangedEventHandler(elementRoot, TreeScope.Children, New StructureChangedEventHandler(AddressOf OnStructureChanged))
備註
eventHandler
可以是 方法的實例,或是 Visual Basic) 中方法 (AddressOf
的參考。