共用方式為


AutomationFactory.GetEvent Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets an object that represents the specified event of the specified Automation server.

Namespace:  System.Runtime.InteropServices.Automation
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Shared Function GetEvent ( _
    automationObject As Object, _
    eventName As String _
) As AutomationEvent
public static AutomationEvent GetEvent(
    Object automationObject,
    string eventName
)

Parameters

  • automationObject
    Type: System.Object
    A reference to the Automation server to retrieve an event for.
  • eventName
    Type: System.String
    The name of the event to retrieve.

Return Value

Type: System.Runtime.InteropServices.Automation.AutomationEvent
An object that represents the specified event.

Remarks

You can use the AutomationEvent returned by this method to handle Automation events with managed event handlers. In Visual Basic, you must use this method and its return value to handle Automation events. In C#, however, you can attach managed event handlers directly to Automation events by using the += syntax, and detach them using the -= syntax. However, in this case, the specified delegate must have a signature that exactly matches the Automation event signature.

For more information, see AutomationEvent. For more information about Automation, see Automation.

NoteNote:

Events with return values are not supported. Additionally, to use C# custom delegates with Automation events that have optional parameters, you must specify default parameter values in the delegate signature. In Visual Basic, delegate parameters cannot be optional; therefore, you must handle the AutomationEvent.EventRaised event instead.

Examples

The following code example demonstrates how to handle Automation events.

This example is part of a larger example in How to: Use Automation in Trusted Applications.

Private Sub SearchEmail()

    UpdateStatusMessage("Searching Inbox for 'Silverlight'...")

    ' The following code demonstrates three ways to handle Automation 
    ' events. In Visual Basic, all three ways use the AutomationEvent class. 

    searchEvent =
        AutomationFactory.GetEvent(outlook, "AdvancedSearchComplete")

    ' The first way is demonstrated by the Handles clause of the 
    ' SearchEvent_EventRaised method, which requires the WithEvents modifier
    ' on the searchEvent variable declaration.

    ' The second way uses the AddHandler syntax with the EventRaised event,
    ' and does not require the WithEvents modifier. 
    ' AddHandler searchEvent.EventRaised, AddressOf SearchEvent_EventRaised

    ' The third way uses the AutomationEvent.AddEventHandler method, and
    ' requires the use of a delegate with an API signature that matches the
    ' Automation event. 
    ' searchEvent.AddEventHandler( 
    '   New AdvancedSearchCompleteDelegate(AddressOf SearchComplete))

    ' Begin the search.
    outlook.AdvancedSearch("Inbox",
        "urn:schemas:mailheader:subject ci_phrasematch 'Silverlight'",
        True, "SubjectSearch")

End Sub

Private WithEvents searchEvent As AutomationEvent

Sub SearchEvent_EventRaised(ByVal sender As Object,
    ByVal e As AutomationEventArgs) Handles searchEvent.EventRaised

    SearchComplete(e.Arguments(0))

End Sub

' Required only with the second two ways of handling Automation events. 
' Private Delegate Sub AdvancedSearchCompleteDelegate(ByRef search As Object)

' Note: Visual Basic does not support the use of custom delegates for 
' events with optional parameters. 

Private Sub SearchComplete(ByRef search As Object)

    Dim searchResults As New List(Of String)
    For Each result As Object In search.Results
        searchResults.Add(result.Subject)
    Next
    SetResultsList(searchResults)

End Sub
private void SearchEmail()
{
    UpdateStatusMessage("Searching Inbox for 'Silverlight'...");

    // The following code demonstrates three ways to handle Automation 
    // events. The first two ways use the AutomationEvent class. 
    // The last two ways require the use of a delegate with 
    // an API signature that matches the Automation event. 

    AutomationEvent searchEvent = AutomationFactory
        .GetEvent(outlook, "AdvancedSearchComplete");

    // The first way: 
    searchEvent.EventRaised += (sender, e) =>
    {
        SearchComplete(e.Arguments[0]);
    };

    // The second way:
    // searchEvent.AddEventHandler(
    //    new AdvancedSearchCompleteDelegate(SearchComplete));

    // The third way:
    // outlook.AdvancedSearchComplete +=
    //    new AdvancedSearchCompleteDelegate(SearchComplete);

    // Begin the search. 
    outlook.AdvancedSearch("Inbox",
        "urn:schemas:mailheader:subject ci_phrasematch 'Silverlight'",
        true, "SubjectSearch");
}

// Required only with the second two ways of handling Automation events. 
private delegate void AdvancedSearchCompleteDelegate(dynamic search);

// To use custom delegates for events with optional parameters, you
// must specify default values in the delegate signature as shown here:
// private delegate void MyEventHandlerDelegate(int i, float f=77);

private void SearchComplete(dynamic search)
{
    List<String> searchResults = new List<String>();
    foreach (dynamic result in search.Results) 
    { 
        searchResults.Add(result.Subject);
    }
    SetResultsList(searchResults);
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.