Share via


WindowClosedEventArgs.GetRuntimeId 方法

定義

擷取與此事件相關聯的消費者介面自動化執行時間識別碼 (識別碼) 。

public:
 cli::array <int> ^ GetRuntimeId();
public int[] GetRuntimeId ();
member this.GetRuntimeId : unit -> int[]
Public Function GetRuntimeId () As Integer()

傳回

Int32[]

引發事件之視窗的消費者介面自動化執行時間識別碼。

範例

在下列範例中,視窗關閉事件的處理常式會確定關閉視窗的執行時間識別碼是否位於開啟視窗的識別碼清單中。 如果執行時間識別碼存在,則會從清單中移除。

/// <summary>
/// Handles window-closed events. Removes the window from the top-level window list.
/// </summary>
/// <param name="sender">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
/// <remarks>
/// runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows.
/// </remarks>
private void WindowClosedHandler(object sender, AutomationEventArgs e)
{
    WindowClosedEventArgs windowEventArgs = (WindowClosedEventArgs)e;
    int[] runtimeIdentifiers = windowEventArgs.GetRuntimeId();
    int index = RuntimeIdListed(runtimeIdentifiers, savedRuntimeIds);
    if (index >= 0)
    {
        savedRuntimeIds.RemoveAt(index);
        Console.WriteLine("Window closed.");
    }
}

/// <summary>
/// Ascertains whether the window is in the list.
/// </summary>
/// <param name="rid">Runtime ID of the window.</param>
/// <returns>Index of the ID in the list, or -1 if it is not listed.</returns>
/// <remarks>
/// runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows.
/// </remarks>
private int RuntimeIdListed(int[] runtimeId, ArrayList runtimeIds)
{
    for (int x = 0; x < runtimeIds.Count; x++)
    {
        int[] listedId = (int[])runtimeIds[x];
        if (Automation.Compare(listedId, runtimeId))
        {
            return x;
        }
    }
    return -1;
}
''' <summary>
''' Handles window-closed events. Removes the window from the top-level window list.
''' </summary>
''' <param name="sender">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
''' <remarks>
''' runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows.
''' </remarks>
Private Sub WindowClosedHandler(ByVal sender As Object, ByVal e As AutomationEventArgs) 
    Dim windowEventArgs As WindowClosedEventArgs = CType(e, WindowClosedEventArgs)
    Dim runtimeIdentifiers As Integer() = windowEventArgs.GetRuntimeId()
    Dim index As Integer = RuntimeIdListed(runtimeIdentifiers, savedRuntimeIds)
    If index >= 0 Then
        savedRuntimeIds.RemoveAt(index)
        Console.WriteLine("Window closed.")
    End If

End Sub


''' <summary>
''' Ascertains whether the window is in the list.
''' </summary>
''' <param name="rid">Runtime ID of the window.</param>
''' <returns>Index of the ID in the list, or -1 if it is not listed.</returns>
''' <remarks>
''' runtimteIds is an ArrayList that contains the runtime IDs of all top-level windows.
''' </remarks>
Private Function RuntimeIdListed(ByVal runtimeId() As Integer, ByVal runtimeIds As ArrayList) As Integer 
    Dim x As Integer
    For x = 0 To runtimeIds.Count - 1
        Dim listedId As Integer() = CType(runtimeIds(x), Integer())
        If Automation.Compare(listedId, runtimeId) Then
            Return x
        End If
    Next x
    Return - 1

End Function 'RuntimeIdListed

備註

當應用程式收到視窗關閉事件時, sender 事件處理常式的 參數無法用來取得已關閉之視窗的相關資訊,因為對應的Microsoft 消費者介面自動化專案已不再有效。 使用 GetRuntimeId 來比對視窗與已知的識別碼。

適用於