System.Windows.Automation.Automation.AddAutomationEventHandler C# (CSharp) Method

AddAutomationEventHandler() public static method

public static AddAutomationEventHandler ( AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler ) : void
eventId AutomationEvent
element AutomationElement
scope TreeScope
eventHandler AutomationEventHandler
return void
        public static void AddAutomationEventHandler(AutomationEvent eventId, AutomationElement element, TreeScope scope, AutomationEventHandler eventHandler)
        {
            Utility.ValidateArgumentNonNull(element, "element");
            Utility.ValidateArgumentNonNull(eventHandler, "eventHandler");
            Utility.ValidateArgument(eventId != AutomationElement.AutomationFocusChangedEvent, "Use FocusChange notification instead");
            Utility.ValidateArgument(eventId != AutomationElement.StructureChangedEvent, "Use StructureChange notification instead");
            Utility.ValidateArgument(eventId != AutomationElement.AutomationPropertyChangedEvent, "Use PropertyChange notification instead");

            try
            {
                BasicEventListener listener = new BasicEventListener(eventId, element, eventHandler);
                Factory.AddAutomationEventHandler(
                    eventId.Id,
                    element.NativeElement,
                    (UIAutomationClient.TreeScope)scope,
                    CacheRequest.CurrentNativeCacheRequest,
                    listener);
                ClientEventList.Add(listener);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
            }
        }

Usage Example

Beispiel #1
0
        public void Z_AutomationEventTest()
        {
            var automationEventsArray = new [] {
                new { Sender = (object)null, Args = (AutomationEventArgs)null }
            };
            var automationEvents = automationEventsArray.ToList();

            automationEvents.Clear();

            AutomationEventHandler handler =
                (o, e) => automationEvents.Add(new { Sender = o, Args = e });

            SelectionItemPattern item1 = (SelectionItemPattern)child1Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern);

            item1.Select();

            AutomationEvent eventId = SelectionItemPattern.ElementSelectedEvent;

            At.AddAutomationEventHandler(eventId,
                                         treeView1Element, TreeScope.Descendants, handler);

            SelectionItemPattern item2 = (SelectionItemPattern)child2Element.GetCurrentPattern(SelectionItemPatternIdentifiers.Pattern);

            item2.Select();
            Thread.Sleep(500);
            At.RemoveAutomationEventHandler(eventId, treeView1Element, handler);
            Assert.AreEqual(1, automationEvents.Count, "event count");
            Assert.AreEqual(child2Element, automationEvents [0].Sender, "event sender");
            Assert.AreEqual(SelectionItemPattern.ElementSelectedEvent, automationEvents [0].Args.EventId, "EventId");
            automationEvents.Clear();

            item1.Select();
            Thread.Sleep(500);
            Assert.AreEqual(0, automationEvents.Count, "event count");
        }
All Usage Examples Of System.Windows.Automation.Automation::AddAutomationEventHandler