示例#1
0
        /// <summary>
        /// given a criteria, find a control within a window
        /// </summary>
        /// <param name="window">the containing window</param>
        /// <param name="criteria">the criteria to find the control</param>
        /// <returns>the found control. null - if not found</returns>
        public IUIItem FindControl(Window window, Dictionary <string, string> criteria)
        {
            // the "all" condition
            SearchCriteria crit = SearchCriteria.All;

            // for each criteria, AND with a new condition
            foreach (string key in criteria.Keys)
            {
                switch (key.ToLower())
                {
                //case Constants.PropertyNames.ControlType:
                //    crit = crit.AndControlType(GetTypeByName(criteria[key]));
                //    break;
                case Constants.PropertyNames.AutomationId:
                    crit = crit.AndAutomationId(criteria[key]);
                    break;

                case Constants.PropertyNames.Text:
                    crit = crit.AndByText(criteria[key]);
                    break;

                case Constants.PropertyNames.ClassName:
                    crit = crit.AndByClassName(criteria[key]);
                    break;

                case Constants.PropertyNames.Index:
                    crit = crit.AndIndex(int.Parse(criteria[key]));
                    break;

                default:
                {
                    bool bNativeFound          = false;
                    AutomationProperty[] props = window.AutomationElement.GetSupportedProperties();
                    foreach (AutomationProperty prop in props)
                    {
                        string propName = prop.ProgrammaticName.Substring(prop.ProgrammaticName.IndexOf('.') + 1);
                        propName = propName.Substring(0, propName.IndexOf("Property"));
                        if (propName.Equals(key, StringComparison.CurrentCultureIgnoreCase))
                        {
                            crit.AndNativeProperty(prop, criteria[key]);
                            bNativeFound = true;
                            break;
                        }
                    }
                    if (bNativeFound)
                    {
                        break;
                    }
                    ;
                }
                    return(null);
                }
                ;
            }

            try
            {
                // search for control with 'crit'
                IUIItem item = window.Get(crit, WaitTime);

                // return the found control
                return(item);
            }
            catch (Exception)
            {
                return(null);
            }
        }