UIAutomation.HasControlInputCmdletBase.SubscribeToEvents C# (CSharp) Method

SubscribeToEvents() protected method

protected SubscribeToEvents ( HasControlInputCmdletBase cmdlet, IUiElement inputObject, UIANET::System.Windows.Automation eventType, UIANET::System.Windows.Automation properties ) : void
cmdlet HasControlInputCmdletBase
inputObject IUiElement
eventType UIANET::System.Windows.Automation
properties UIANET::System.Windows.Automation
return void
        protected internal void SubscribeToEvents(HasControlInputCmdletBase cmdlet,
                                                  IUiElement inputObject,
                                                  classic.AutomationEvent eventType,
                                                  classic.AutomationProperty[] properties)
        {
            if (null == CurrentData.Events) {
                CurrentData.InitializeEventCollection();
            }
            
            try {
                var cacheRequest = new classic.CacheRequest
                // CacheRequest cacheRequest = new CacheRequest
                {
                    AutomationElementMode = classic.AutomationElementMode.Full,
                    // 20140130
                    // TreeFilter = Automation.RawViewCondition
                    TreeFilter = UiaAutomation.RawViewCondition
                };
                cacheRequest.Add(classic.AutomationElement.NameProperty);
                cacheRequest.Add(classic.AutomationElement.AutomationIdProperty);
                cacheRequest.Add(classic.AutomationElement.ClassNameProperty);
                cacheRequest.Add(classic.AutomationElement.ControlTypeProperty);
                //cacheRequest.Add(AutomationElement.ProcessIdProperty);
                // cache patterns?
                
                // cacheRequest.Activate();
                cacheRequest.Push();
                
                classic.AutomationEventHandler uiaEventHandler;
                switch (eventType.ProgrammaticName) {
                    case "InvokePatternIdentifiers.InvokedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.InvokePattern.InvokedEvent,
                            inputObject,
                            classic.TreeScope.Element,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextChangedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.TextPattern.TextChangedEvent,
                            inputObject,
                            classic.TreeScope.Element,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "TextPatternIdentifiers.TextSelectionChangedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.TextPattern.TextSelectionChangedEvent,
                            inputObject,
                            classic.TreeScope.Element,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowOpenedProperty":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.WindowPattern.WindowOpenedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationPropertyChangedEvent":
                        if (properties != null) {
                            classic.AutomationPropertyChangedEventHandler uiaPropertyChangedEventHandler;
                            UiaAutomation.AddAutomationPropertyChangedEventHandler(
                                inputObject,
                                classic.TreeScope.Subtree,
                                uiaPropertyChangedEventHandler = 
                                    new classic.AutomationPropertyChangedEventHandler(cmdlet.AutomationPropertyChangedEventHandler),
                                properties);
                            UiaHelper.WriteEventToCollection(cmdlet, uiaPropertyChangedEventHandler);
                            if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaPropertyChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        }
                        break;
                    case "AutomationElementIdentifiers.StructureChangedEvent":
                        classic.StructureChangedEventHandler uiaStructureChangedEventHandler;
                        UiaAutomation.AddStructureChangedEventHandler(
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaStructureChangedEventHandler = 
                            new classic.StructureChangedEventHandler(cmdlet.StructureChangedEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaStructureChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaStructureChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "WindowPatternIdentifiers.WindowClosedProperty":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.WindowPattern.WindowClosedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuClosedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.AutomationElement.MenuClosedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.MenuOpenedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.AutomationElement.MenuOpenedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipClosedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.AutomationElement.ToolTipClosedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.ToolTipOpenedEvent":
                        UiaAutomation.AddAutomationEventHandler(
                            classic.AutomationElement.ToolTipOpenedEvent,
                            inputObject,
                            classic.TreeScope.Subtree,
                            uiaEventHandler = new classic.AutomationEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    case "AutomationElementIdentifiers.AutomationFocusChangedEvent":
                        classic.AutomationFocusChangedEventHandler uiaFocusChangedEventHandler;
                        UiaAutomation.AddAutomationFocusChangedEventHandler(
                            uiaFocusChangedEventHandler = new classic.AutomationFocusChangedEventHandler(cmdlet.AutomationEventHandler));
                        UiaHelper.WriteEventToCollection(cmdlet, uiaFocusChangedEventHandler);
                        if (cmdlet.PassThru) { cmdlet.WriteObject(cmdlet, uiaFocusChangedEventHandler); } else { cmdlet.WriteObject(cmdlet, true); }
                        break;
                    default:
                        WriteVerbose(cmdlet, 
                                     "the following event has not been subscribed to: " + 
                                     eventType.ProgrammaticName);
                        break;
                }
                cacheRequest.Pop();
                
            } 
            catch (Exception e) {
                
                WriteVerbose(cmdlet,
                              "Unable to register event handler " +
                              eventType.ProgrammaticName +
                              " for " +
                    // 20140312
                              // inputObject.Current.Name);
                    inputObject.GetCurrent().Name);
                 WriteVerbose(cmdlet,
                              e.Message);
            }
        }
        #endregion subscribe to events