AddEventHandler Method
Collapse the table of content
Expand the table of content

AutomationEvent.AddEventHandler Method

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Attaches the specified delegate to the Automation event.

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

public void AddEventHandler(
	Delegate handler
)

Parameters

handler
Type: System.Delegate
The delegate to attach to the event.

This method requires the attached delegate to have a signature that exactly matches the Automation event signature. Alternately, you can use the EventRaised event, which does not require knowledge of the Automation event signature.

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, so you must handle the AutomationEvent.EventRaised event instead.

The following code example demonstrates how to handle Automation events.


private void SearchEmail()
{
    UpdateStatusMessage("Searching Inbox for 'WindowsPhone'...");

    // 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 'WindowsPhone'",
        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);
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1

Windows Phone

Show:
© 2017 Microsoft