private void RegisterEventListener()

in src/Desktop/UIAutomation/EventHandlers/EventListenerFactory.cs [341:473]


        private void RegisterEventListener(EventListenerFactoryMessage msgData)
        {
            try
            {
                EventMessage m = null;

                var win32Helper = new Win32Helper();

                switch (msgData.EventId)
                {
                    case EventType.UIA_AutomationFocusChangedEventId:
                        if (this.EventListenerFocusChanged == null)
                        {
                            var uia = (IUIAutomation) UIAutomation8 ?? UIAutomation;
                            this.EventListenerFocusChanged = new FocusChangedEventListener(uia, msgData.Listener);
                        }
                        break;
                    case EventType.UIA_StructureChangedEventId:
                        if (this.EventListenerStructureChanged == null)
                        {
                            this.EventListenerStructureChanged = new StructureChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener);
                        }
                        break;
                    case EventType.UIA_AutomationPropertyChangedEventId:
                        if (this.EventListenerPropertyChanged == null)
                        {
                            this.EventListenerPropertyChanged = new PropertyChangedEventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.Listener,msgData.Properties);
                        }
                        break;
                    case EventType.UIA_TextEdit_TextChangedEventId:
                        if (this.EventListenerTextEditTextChanged == null)
                        {
                            this.EventListenerTextEditTextChanged = new TextEditTextChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener);
                        }
                        break;
                    case EventType.UIA_ChangesEventId:
                        if (this.EventListenerChanges == null)
                        {
                            this.EventListenerChanges = new ChangesEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener);
                        }
                        break;
                    case EventType.UIA_NotificationEventId:
                        if (win32Helper.IsWindowsRS3OrLater())
                        {
                            if (this.EventListenerNotification == null)
                            {
                                this.EventListenerNotification = new NotificationEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener);
                            }
                        }
                        else
                        {
#pragma warning disable CA2000 // Call IDisposable.Dispose()
                            m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null);
#pragma warning restore CA2000

                            m.Properties = new List<KeyValuePair<string, dynamic>>()
                            {
                                new KeyValuePair<string, dynamic>("Message", "Event listener registration is rejected."),
                                new KeyValuePair<string, dynamic>("Event Id", msgData.EventId),
                                new KeyValuePair<string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)),
                                new KeyValuePair<string, dynamic>("Reason", "Not supported platform"),
                            };
                            msgData.Listener(m);
                        }
                        break;
                    case EventType.UIA_ActiveTextPositionChangedEventId:
                        if (win32Helper.IsWindowsRS5OrLater())
                        {
                            if (this.EventListenerNotification == null)
                            {
                                this.EventListenerActiveTextPositionChanged = new ActiveTextPositionChangedEventListener(this.UIAutomation8, this.RootElement.PlatformObject, this.Scope, msgData.Listener);
                            }
                        }
                        else
                        {
#pragma warning disable CA2000 // Call IDisposable.Dispose()
                            m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null);
#pragma warning restore CA2000

                            m.Properties = new List<KeyValuePair<string, dynamic>>()
                            {
                                new KeyValuePair<string, dynamic>("Message", "Event listener registration is rejected."),
                                new KeyValuePair<string, dynamic>("Event Id", msgData.EventId),
                                new KeyValuePair<string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)),
                                new KeyValuePair<string, dynamic>("Reason", "Not supported platform"),
                            };
                            msgData.Listener(m);
                        }
                        break;
                    default:
                        if (this.EventListeners.ContainsKey(msgData.EventId) == false)
                        {
                            this.EventListeners.Add(msgData.EventId, new EventListener(this.UIAutomation, this.RootElement.PlatformObject, this.Scope, msgData.EventId, msgData.Listener));
                        }
                        break;
                }

#pragma warning disable CA2000 // Call IDisposable.Dispose()
                m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null);
#pragma warning restore CA2000

                m.Properties = new List<KeyValuePair<string, dynamic>>()
                {
                    new KeyValuePair<string, dynamic>("Message", "Succeeded to register an event listener"),
                    new KeyValuePair<string, dynamic>("Event Id", msgData.EventId),
                    new KeyValuePair<string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)),
                };
                msgData.Listener(m);
                if(msgData.EventId == EventType.UIA_AutomationFocusChangedEventId)
                {
                    this.EventListenerFocusChanged.ReadyToListen = true;
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();

#pragma warning disable CA2000 // Call IDisposable.Dispose()
                var m = EventMessage.GetInstance(EventType.UIA_EventRecorderNotificationEventId, null);
#pragma warning restore CA2000

                m.Properties = new List<KeyValuePair<string, dynamic>>()
                {
                    new KeyValuePair<string, dynamic>("Message", "Failed to register an event listener"),
                    new KeyValuePair<string, dynamic>("Event Id", msgData.EventId),
                    new KeyValuePair<string, dynamic>("Event Name", EventType.GetInstance().GetNameById(msgData.EventId)),
                    new KeyValuePair<string, dynamic>("Error", e.Message)
                };
                msgData.Listener(m);
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }