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

AddStructureChangedEventHandler() public static method

public static AddStructureChangedEventHandler ( AutomationElement element, TreeScope scope, StructureChangedEventHandler eventHandler ) : void
element AutomationElement
scope TreeScope
eventHandler StructureChangedEventHandler
return void
        public static void AddStructureChangedEventHandler(AutomationElement element, TreeScope scope, StructureChangedEventHandler eventHandler)
        {
            Utility.ValidateArgumentNonNull(element, "element");
            Utility.ValidateArgumentNonNull(eventHandler, "eventHandler");
            Utility.ValidateArgumentNonNull(eventHandler, "eventHandler");

            try
            {
                StructureEventListener listener = new StructureEventListener(AutomationElement.StructureChangedEvent, element, eventHandler);
                Factory.AddStructureChangedEventHandler(
                    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

Exemplo n.º 1
0
        public void StructureEventTest()
        //this test also tested WindowPattern.WindowOpenedEvent (i.e. AddAutomationEventHandler)
        {
            int                    automationEventCount = 0;
            int                    structureEventCount  = 0;
            AutomationEvent        eventId = null;
            AutomationEventHandler automationEventHandler = (o, e) =>
            {
                automationEventCount++;
                eventId = e.EventId;
            };

            At.AddAutomationEventHandler(
                WindowPattern.WindowOpenedEvent,
                AutomationElement.RootElement, TreeScope.Children,
                automationEventHandler);
            StructureChangedEventHandler structureEventHandler = (o, e) =>
            {
                if (e.StructureChangeType == StructureChangeType.ChildAdded)
                {
                    structureEventCount++;
                }
            };

            At.AddStructureChangedEventHandler(
                AutomationElement.RootElement, TreeScope.Children, structureEventHandler);
            int pid = OpenForm();

            Thread.Sleep(3000);
            Assert.AreEqual(1, structureEventCount, "[OpenForm] count of StructureChangedEvent");
            Assert.AreEqual(1, automationEventCount, "[OpenForm] count of WindowOpenedEvent");
            Assert.AreEqual(WindowPattern.WindowOpenedEvent, eventId);

            automationEventCount = 0;
            structureEventCount  = 0;
            At.RemoveAllEventHandlers();
            int pid2 = OpenForm();

            Thread.Sleep(3000);
            Assert.AreEqual(0, structureEventCount);
            Assert.AreEqual(0, automationEventCount);

            structureEventHandler = (o, e) =>
            {
                structureEventCount++;
            };
            At.AddStructureChangedEventHandler(
                AutomationElement.RootElement, TreeScope.Children, structureEventHandler);
            CloseForm(pid);
            CloseForm(pid2);
            Thread.Sleep(3000);
            // Note: I expect 2 events here (whose StructureChangeType are both ChildRemoved)
            // But as tested on Win 7, we'll actually get no event,
            // And with our current implementation, we'll get 4 events (i.e. besides the 2 expected events, we
            // get other 2 ChildRemoved events, whose sender is the "testFormElement")
            Assert.AreEqual(0, structureEventCount, "[CloseForm] count of StructureChangedEvent");
        }
All Usage Examples Of System.Windows.Automation.Automation::AddStructureChangedEventHandler