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; } } }
public static void RemoveAutomationEventHandler(AutomationEvent eventId, AutomationElement element, 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 = (BasicEventListener)ClientEventList.Remove(eventId, element, eventHandler); Factory.RemoveAutomationEventHandler(eventId.Id, element.NativeElement, listener); } catch (System.Runtime.InteropServices.COMException e) { Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; } } }