語言

InvokePattern.Invoke 方法

定義

發送啟動控制並啟動單一且明確行動的請求。

public:
 void Invoke();
public void Invoke();
member this.Invoke : unit -> unit
Public Sub Invoke ()

例外狀況

該元素不支援 InvokePattern 控制模式,或被隱藏或阻擋。

該元素尚未啟用。 當使用者介面自動化提供者自行執行 IsEnabled 屬性處理時,即可提出此問題。

範例

以下範例 InvokePattern 中,從一個控制點獲得一個控制模式,並呼叫該 Invoke 方法。

///--------------------------------------------------------------------
/// <summary>
/// Obtains an InvokePattern control pattern from a control
/// and calls the InvokePattern.Invoke() method on the control.
/// </summary>
/// <param name="targetControl">
/// The control of interest.
/// </param>
///--------------------------------------------------------------------
private void InvokeControl(AutomationElement targetControl)
{
    InvokePattern invokePattern = null;

    try
    {
        invokePattern =
            targetControl.GetCurrentPattern(InvokePattern.Pattern)
            as InvokePattern;
    }
    catch (ElementNotEnabledException)
    {
        // Object is not enabled
        return;
    }
    catch (InvalidOperationException)
    {
        // object doesn't support the InvokePattern control pattern
        return;
    }

    invokePattern.Invoke();
}
'''--------------------------------------------------------------------
''' <summary>
''' Obtains an InvokePattern control pattern from a control
''' and calls the InvokePattern.Invoke() method on the control.
''' </summary>
''' <param name="targetControl">
''' The control of interest.
''' </param>
'''--------------------------------------------------------------------
Private Sub InvokeControl(ByVal targetControl As AutomationElement) 
    Dim invokePattern As InvokePattern = Nothing
    
    Try
        invokePattern = _
        DirectCast(targetControl.GetCurrentPattern(invokePattern.Pattern), _
        InvokePattern)
    Catch e As ElementNotEnabledException
        ' Object is not enabled.
        Return
    Catch e As InvalidOperationException
        ' Object doesn't support the InvokePattern control pattern
        Return
    End Try
    
    invokePattern.Invoke()

End Sub

備註

Invoke呼叫應立即回撥,且不會被阻擋。 然而,這種行為完全取決於Microsoft 使用者介面自動化提供者的實作方式。 在呼叫 Invoke 造成阻塞問題(如模態對話)的情況下,可能需要另外一個輔助執行緒來呼叫該方法。

適用於

另請參閱