通过


InvokePattern.Invoke 方法

定义

发送一个请求来激活控件并启动其单个明确作。

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

例外

该元素不支持 InvokePattern 控件模式或被隐藏或阻止。

未启用该元素。 当UI 自动化提供程序已实现自己对 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 UI 自动化提供程序实现。 在调用 Invoke 会导致阻塞问题(如模式对话)的情况下,可能需要单独的帮助程序线程来调用该方法。

适用于

另请参阅