IInvokeProvider.Invoke 方法

定義

傳送要求以啟動控制項,並啟始其單一明確的動作。

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

例外狀況

如果未啟用此控制項。

範例

下列範例會在 Invoke 控制項的 MouseDown 事件處理常式上實作 方法。 providerControl假設 是建構 類別時初始化的成員變數。

/// <summary>
/// Responds to an InvokePattern.Invoke by simulating a MouseDown event.
/// </summary>
/// <remarks>
/// ProviderControl is a button control object that also implements 
/// IRawElementProviderSimple.
/// </remarks>
void IInvokeProvider.Invoke()
{
    // If the control is not enabled, we're responsible for letting UIAutomation know.
    // It catches the exception and then throws it to the client.
    if (false == (bool)rawElementProvider.GetPropertyValue(AutomationElementIdentifiers.IsEnabledProperty.Id))
    {
        throw new ElementNotEnabledException();
    }

    // Create arguments for the event. The parameters aren't used.
    MouseEventArgs mouseArgs = new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0);

    // Invoke the MouseDown handler. We cannot call MyControl_MouseDown directly, 
    // because it is illegal to update the UI from a different thread.
    MouseEventHandler onMouseEvent = ProviderControl.RootButtonControl_MouseDown;
    ProviderControl.BeginInvoke(onMouseEvent, new object[] { this, mouseArgs });
    }
}

備註

Invoke 為非同步呼叫,且必須立即返回,不可封鎖。

注意

對於叫用時直接或間接啟動強制回應對話方塊的控制項,此行為尤其重要。 任何引發事件的使用者介面自動化用戶端都會維持封鎖的狀態,直到強制回應對話方塊關閉為止。

Invoke 會引發 InvokedEvent 事件。 可能的話,應該在控制項完成相關聯的動作之後引發 事件。

InvokedEvent 應在下列案例中維護 Invoke 要求之前引發:

  • 無法或不適合等待至動作完成。

  • 動作需要使用者互動。

  • 此動作相當耗時,而且會導致呼叫用戶端封鎖很長的時間。

適用於

另請參閱