UI Automation-MultipleViewPattern

来源:互联网 发布:手机nfc读身份证软件 编辑:程序博客网 时间:2024/06/05 04:55

MultipleViewPattern控件模式用于支持控件,控件提供了同一组信息或子控件的多个表示形式,并且可以在这些表示形式之间进行切换。常用两种方法,GetViewName用来检索特定于控件的视图的名称,还有SetCurrentView,用来设置当前的特定于控件的视图。

示例:

        public static void TestMultipleViewpattern()

        {

            Process adressbook = Process.Start(@"C:/Program Files/Outlook Express/wab.exe");

            Thread.Sleep(2000);

 

            //Get main window   "Address Book - Main Identity"

 

            AutomationElement addresswindow = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address Book - Main Identity"));

            Thread.Sleep(2000);

 

            AutomationElement list = Utility.UIA.FindAutomationElement.ByAutomationID(addresswindow, "9001",true);

            MultipleViewPattern listpattern = Utility.UIA.ControlPattern.MultipleViewpattern.GetMultipleViewPattern(list);

            if (listpattern.Current.CurrentView != 3)

            {

                //get view menu item

                AutomationElement view = Utility.UIA.FindAutomationElement.ByName(addresswindow, "View", false);

                ExpandCollapsePattern viewpattern = Utility.UIA.ControlPattern.ExpandCollapsepattern.GetExpandCollapsePattern(view);

                viewpattern.Expand();

                Thread.Sleep(2000);

 

                //get list menu item

                AutomationElement listitem = Utility.UIA.FindAutomationElement.ByName(addresswindow, "List", false);

                InvokePattern listpattern1 = Utility.UIA.ControlPattern.Invokepattern.GetInvokePattern(listitem);

                listpattern1.Invoke();

 

            }

 

 

        }

 

public static MultipleViewPattern GetMultipleViewPattern(AutomationElement element)

        {

            object currentPattern;

            if (!element.TryGetCurrentPattern(MultipleViewPattern.Pattern, out currentPattern))

            {

                throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the DockPattern.",

                    element.Current.AutomationId, element.Current.Name));

            }

            return currentPattern as MultipleViewPattern;

        }

 

 

原创粉丝点击