AutomationElement显示了使用Inspect.exe但使用UIAutomationCore.dll或System.Windows.Automation时,确实显示不起来 [英] AutomationElement shows up using Inspect.exe but does show not up when using UIAutomationCore.dll or System.Windows.Automation

查看:5349
本文介绍了AutomationElement显示了使用Inspect.exe但使用UIAutomationCore.dll或System.Windows.Automation时,确实显示不起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR :我在做什么错误导致该工作区窗格显示在检查对象,但在我的自定义代码没有显示出来。






我试图写一些UI自动化第三方程序。我使用的 Inspect.exe 随附与 Windows SDK的,我曾经尝试都的System.Windows.Automation 和直接的 COM调用(使用 UIA验证)。

 过程[] =流程Process.GetProcessesByName(绝密客户); 
如果(processes.Length == 0)抛出新的异常(无法找到\绝密Client\流程);

PropertyCondition parentFileCond =新PropertyCondition(AutomationElement.ProcessIdProperty,流程[0] .ID);
PropertyCondition workspaceCond =新PropertyCondition(AutomationElement.NameProperty,工作区,PropertyConditionFlags.IgnoreCase);
PropertyCondition documentCond =新PropertyCondition(AutomationElement.NameProperty,Untitled3,PropertyConditionFlags.IgnoreCase);

VAR parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,parentFileCond);
VAR workspaceElement = parentElement.FindFirst(TreeScope.Children,workspaceCond); //也不会TreeScope.Descendants
VAR documentElement = workspaceElement.FindFirst(TreeScope.Children,documentCond)工作;

当我尝试上面的代码, parentElement 确实有正确的引用主程序窗口,但 workspaceElement 为null。






的临时解决方法:



如果我改变我的 documentElement 代码:

  VAR documentElement = parentElement.FindFirst(TreeScope.Descendants,documentCond); 



我会得到正确的元素返回。由于文档窗口是一个我真的想反正我可以用这个作为一个解决办法,但我想知道为什么工作区窗格中就不会显示出来,所以我可以提高我的技能的情况下,我与运行到这个在未来。一种情况,我不能变通解决






更新:我试过MrGomez的建议

  PropertyCondition parentFileCond =新PropertyCondition(AutomationElement.ProcessIdProperty,5872); 
PropertyCondition panelCond =新PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Pane);

VAR parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,parentFileCond);
VAR板= parentElement.FindAll(TreeScope.Children,panelCond);



在这里输入的形象描述



我得到3个结果,不幸的是我有4个小组,和一个没有露面被评为面板工作



我也试过使用的 TreeWalker

  PropertyCondition parentFileCond =新PropertyCondition(AutomationElement.ProcessIdProperty,5872); 
PropertyCondition workspaceCond =新PropertyCondition(AutomationElement.NameProperty工作区);

变种沃克=新TreeWalker(workspaceCond);
VAR parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,parentFileCond);
VAR workspaceElement = walker.Normalize(parentElement);



但也返回null workspaceElement



最后在绝望中我尝试了NativeWindowHandle从检查的当前值,并开始从根节点走去。

  PropertyCondition workspaceCond =新PropertyCondition(AutomationElement.NativeWindowHandleProperty,0x110906); 
变种沃克=新TreeWalker(workspaceCond);
VAR workspaceElement = walker.Normalize(AutomationElement.RootElement);

工作空间元素的 STILL 的空。






个结果发现



我终于做到让工作空间展现出来,但我不得不执行

  PropertyCondition workspaceCond =新PropertyCondition(AutomationElement.NativeWindowHandleProperty,0x110906); 
VAR测试= AutomationElement.RootElement.FindFirst(TreeScope.Subtree,workspaceCond);并花了相当长的时间来运行。



旧屏幕捕获



下面是screneshot从Inspect.exe显示树视图。





下面是该程序的主窗口的属性。

 如何发现:从树中选择... 
RuntimeId:[42.2557552]
BoundingRectangle:{1:76 T:1 R:1311 A:1003}
的ProcessID :8160
ControlType:UIA_WindowControlTypeId(0xC370)
LocalizedControlType:窗口,
名称:绝密
ACCESSKEY:
HasKeyboardFocus:假
IsKeyboardFocusable :真正的
IsEnabled:真正的
类名:C:\Program文件(x86)\RedactedProgramFiles7\RedactedClientFolder
帮助文件:
IsPassword:假
NativeWindowHandle:0x270670
IsOffscreen:假
FrameworkId:Win32的
ProviderDescription:[PID:4000,HWND:0x270670主营:嵌套[PID:8160,HWND:0x270670注释(父链接):微软:注释代理(非托管:uiautomationcore.dll);主营:微软:MSAA代理(非托管:uiautomationcore.dll)];非工作:微软:非客户端代理(非托管:uiautomationcore.dll); HWND(父链接):微软:HWND代理(非托管:uiautomationcore.dll)]
Window.CanMaximize:真正的
Window.CanMinimize:真正的
Window.WindowVisualState:正常(0)
Window.WindowInteractionState:ReadyForUserInteraction(2)
Window.IsModal:假
Window.IsTopmost:假
Transform.CanMove:真正的
Transform.CanResize:真正的
Transform.CanRotate:假
LegacyIAccessible.ChildId:0
LegacyIAccessible.DefaultAction:
LegacyIAccessible.Description:
LegacyIAccessible.Help:
LegacyIAccessible .KeyboardShortcut:
LegacyIAccessible.Name:绝密
LegacyIAccessible.Role:客户端(是0xA)
LegacyIAccessible.State:聚焦的(0x100000处)
LegacyIAccessible.Value:
IsDockPatternAvailable:假
IsExpandCollapsePatternAvailable:假
IsGridItemPatternAvailable:假
IsGridPatternAvailable:假
IsInvokePatternAvailable:假
IsLegacyIAccessiblePatternAvailable:真正的
IsMultipleViewPatternAvailable:假
IsRangeValuePatternAvailable:假
IsScrollPatternAvailable:假
IsScrollItemPatternAvailable:假
IsSelectionItemPatternAvailable:假
IsSelectionPatternAvailable:假
IsTablePatternAvailable:假
IsTableItemPatternAvailable:假
IsTextPatternAvailable:假
IsTogglePatternAvailable:假
IsTransformPatternAvailable:真正的
IsValuePatternAvailable:假
IsWindowPatternAvailable:真正的
IsItemContainerPatternAvailable:假
IsVirtualizedItemPatternAvailable:假
IsSynchronizedInputPatternAvailable:假
则firstChild:工作区窗格中的
LastChild:应用程序菜单栏
下一页:检查(HWND:0x01700F06)窗口
上:沙盒控制台(调试) - 微软的Visual Studio(管理员)窗口
等道具:对象没有附加属性
孩子:工作区窗格中的
(空)标题栏
应用程序菜单酒吧
祖先:桌面窗格中的
[无父]

下面是。问题的工作区窗格中的属性。

 如何发现:从树中选择... 
RuntimeId: [42.34146524]
BoundingRectangle:{1:82 T:51 R:1303 A:995}
进程ID:8160
ControlType:UIA_PaneControlTypeId(0xC371)
LocalizedControlType:窗格
名称:工作区
ACCESSKEY:
HasKeyboardFocus:假
IsKeyboardFocusable:真正的
IsEnabled:真正的
类名MDICLIENT
帮助文件:
IsPassword:假
NativeWindowHandle:0x20908DC
IsOffscreen:假
FrameworkId:Win32的
ProviderDescription:[PID:4000,HWND:0x20908DC主营:嵌套[PID:8160,HWND:0x20908DC注释(父链接):微软:注释代理(非托管:uiautomationcore.dll);主营:微软:MSAA代理(非托管:uiautomationcore.dll)]; HWND(父链接):微软:HWND代理(非托管:uiautomationcore.dll)]
LegacyIAccessible.ChildId:0
LegacyIAccessible.DefaultAction:
LegacyIAccessible.Description:
LegacyIAccessible.Help:
LegacyIAccessible.KeyboardShortcut:
LegacyIAccessible.Name:工作区
LegacyIAccessible.Role:客户端(是0xA)
LegacyIAccessible.State:聚焦的(0x100000处)
LegacyIAccessible.Value:
IsDockPatternAvailable:假
IsExpandCollapsePatternAvailable:假
IsGridItemPatternAvailable:假
IsGridPatternAvailable:假
IsInvokePatternAvailable:假
IsLegacyIAccessiblePatternAvailable:真正的
IsMultipleViewPatternAvailable:假
IsRangeValuePatternAvailable:假
IsScrollPatternAvailable:假
IsScrollItemPatternAvailable:假
IsSelectionItemPatternAvailable:假
IsSelectionPatternAvailable:假
IsTablePatternAvailable:假
IsTableItemPatternAvailable:假
IsTextPatternAvailable:假
IsTogglePatternAvailable:假
IsTransformPatternAvailable:假
IsValuePatternAvailable:假
IsWindowPatternAvailable:假
IsItemContainerPatternAvailable:假
IsVirtualizedItemPatternAvailable:假
IsSynchronizedInputPatternAvailable:假
则firstChild:Untitled3窗口
LastChild:信件(共32638 32638):窗口
下一:( NULL)标题栏
上一个:[空]
等道具:对象没有附加属性
孩子:Untitled3窗口
字母(的32638 32638):窗口
祖先:绝密窗口
桌面窗格中的
[无父]


$ b $ :b

下面是工作文件窗口的属性。

 如何发现:从一棵树... $选择b $ b RuntimeId:[42.9505096]
BoundingRectangle:{1:86 T:53 R:651乙:491}
进程ID:8160
ControlType:UIA_WindowControlTypeId(0xC370)
LocalizedControlType:窗口,
名称:Untitled3
ACCESSKEY:
HasKeyboardFocus:假
IsKeyboardFocusable:真正的
IsEnabled:真正的
的AutomationID: 10
类名:ProToolsSubMDIWndClass
帮助文件:
IsPassword:假
NativeWindowHandle:0x910948
IsOffscreen:假
FrameworkId:的Win32
ProviderDescription:[PID:4000,HWND:0x910948主营:嵌套[PID:8160,HWND:0x910948注释(父链接):微软:注释代理(非托管:uiautomationcore.dll);主营:微软:MSAA代理(非托管:uiautomationcore.dll)];非工作:微软:非客户端代理(非托管:uiautomationcore.dll); HWND(父链接):微软:HWND代理(非托管:uiautomationcore.dll)]
Window.CanMaximize:真正的
Window.CanMinimize:真正的
Window.WindowVisualState:正常(0)
Window.WindowInteractionState:ReadyForUserInteraction(2)
Window.IsModal:假
Window.IsTopmost:假
Transform.CanMove:真正的
Transform.CanResize:真正的
Transform.CanRotate:假
LegacyIAccessible.ChildId:0
LegacyIAccessible.DefaultAction:
LegacyIAccessible.Description:
LegacyIAccessible.Help:
LegacyIAccessible .KeyboardShortcut:
LegacyIAccessible.Name:Untitled3
LegacyIAccessible.Role:客户端(是0xA)
LegacyIAccessible.State:聚焦的(0x100000处)
LegacyIAccessible.Value:
IsDockPatternAvailable:假
IsExpandCollapsePatternAvailable:假
IsGridItemPatternAvailable:假
IsGridPatternAvailable:假
IsInvokePatternAvailable:假
IsLegacyIAccessiblePatternAvailable:真正的
IsMultipleViewPatternAvailable:假
IsRangeValuePatternAvailable:假
IsScrollPatternAvailable:假
IsScrollItemPatternAvailable:假
IsSelectionItemPatternAvailable:假
IsSelectionPatternAvailable:假
IsTablePatternAvailable:假
IsTableItemPatternAvailable:假
IsTextPatternAvailable:假
IsTogglePatternAvailable:假
IsTransformPatternAvailable:真正的
IsValuePatternAvailable:假
IsWindowPatternAvailable:真正的
IsItemContainerPatternAvailable:假
IsVirtualizedItemPatternAvailable:假
IsSynchronizedInputPatternAvailable:假
则firstChild:拇指
LastChild:(空)标题栏
下一页:信件(共32638 32638):窗口
上一个:[空]
。其他道具:对象没有附加属性
孩子:拇指
(空)标题栏
祖先:工作区窗格中的
绝密窗口
桌面窗格中的
[无父]


解决方案

非常好的问题。根据您所记录的问题,很显然,你的条件:

  PropertyCondition workspaceCond =新PropertyCondition(
AutomationElement .NameProperty,工作区,PropertyConditionFlags.IgnoreCase);



...失败的评价。为什么呢?



答案是如何您的条件进行评估。看你的元素树,我们注意到这个属性为工作

  IsWindowPatternAvailable:假

和主窗口和 Untitled3

  IsWindowPatternAvailable:真正的

从MSDN




  UIA_IsWindowPatternAvailablePropertyId 30044 

标识 IsWindowPatternAvailable 属性,指示窗口控制模式是否适用于自动化元素。如果 TRUE ,客户端可以检索的 IUIAutomationWindowPattern 从元素接口。




我们找到一个摄制的在这个线程,这意味着相同的故障模式作为当前遇到的人。我们还注意到缺少窗口属性目前这一元素,因为 IUIAutomationWindowPattern 不可访问。



一个解决方法是可从的 aforelinked线程。相反 PropertyCondition ,一种可能是:



 公共类ConditionMatcher:IMatchConditions 
{
公共BOOL匹配(AutomationElement元,成色情况):
{
返回新TreeWalker(条件).Normalize(元)!= NULL;
}
}



或者交替,人们可能会使用替代方法已经给定,前提是您的树结构是保证浅(因此,适当这个网站的名字,不会触发栈溢出)。



诚然,这不是最明显的问题。在完美的世界,MSDN的的有关于这个主题更好的文档。


TL;DR: What am I doing wrong that is causing the workspace pane to show up in Inspect Objects but not show up in my custom code?


I am trying to write some UI automation to a 3rd party program. I am using Inspect.exe that came with the Windows SDK, and I have tried both System.Windows.Automation and direct COM Calls (using the wrapper library from UIA Verify).

Process[] processes = Process.GetProcessesByName("Redacted Client");
if (processes.Length == 0) throw new Exception("Could not find \"Redacted Client\" process");

PropertyCondition parentFileCond = new PropertyCondition(AutomationElement.ProcessIdProperty, processes[0].Id);
PropertyCondition workspaceCond = new PropertyCondition(AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);
PropertyCondition documentCond = new PropertyCondition(AutomationElement.NameProperty, "Untitled3", PropertyConditionFlags.IgnoreCase);

var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentFileCond);
var workspaceElement = parentElement.FindFirst(TreeScope.Children, workspaceCond); //Also does not work with TreeScope.Descendants
var documentElement = workspaceElement.FindFirst(TreeScope.Children, documentCond);

When I try the above code, parentElement does have the correct reference to the main program window, but workspaceElement is null.


A temporary work around:

If I change my documentElement code to:

var documentElement = parentElement.FindFirst(TreeScope.Descendants, documentCond);

I will get the correct element returned. I can use this as a workaround as the document window is the one I really wanted anyway, but I would like to know why the Workspace pane would not show up so I can improve my skills in case I run in to this in the future with a situation I cannot work around.


UPDATE: I tried MrGomez's suggestions

PropertyCondition parentFileCond = new PropertyCondition(AutomationElement.ProcessIdProperty, 5872);
PropertyCondition panelCond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane);

var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentFileCond);
var panels = parentElement.FindAll(TreeScope.Children, panelCond);

I get 3 results, unfortunately I have 4 panels, and the one that did not show up was the panel named Workspace.

I also tried to use a TreeWalker

PropertyCondition parentFileCond = new PropertyCondition(AutomationElement.ProcessIdProperty, 5872);
PropertyCondition workspaceCond= new PropertyCondition(AutomationElement.NameProperty, "Workspace");

var walker = new TreeWalker(workspaceCond);
var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentFileCond);
var workspaceElement = walker.Normalize(parentElement);

but that also returns null for workspaceElement

Finally in desperation I tried the current value of "NativeWindowHandle" from Inspect and started the walked from the root node.

PropertyCondition workspaceCond = new PropertyCondition(AutomationElement.NativeWindowHandleProperty, 0x110906);
var walker = new TreeWalker(workspaceCond);
var workspaceElement = walker.Normalize(AutomationElement.RootElement);

Workspace element is STILL null.


Result Found

I finally did get Workspace to show up, but I had to perform

PropertyCondition workspaceCond = new PropertyCondition(AutomationElement.NativeWindowHandleProperty, 0x110906);
var test = AutomationElement.RootElement.FindFirst(TreeScope.Subtree, workspaceCond); and it took quite a while to run.

Old Screen Captures

Here is screneshot from Inspect.exe showing the tree view.

Here are the properties of the main window of the program.

How found:  Selected from tree...
RuntimeId:  "[42.2557552]"
BoundingRectangle:  {l:75 t:1 r:1311 b:1003}
ProcessId:  8160
ControlType:    UIA_WindowControlTypeId (0xC370)
LocalizedControlType:   "window"
Name:   "Redacted"
AccessKey:  ""
HasKeyboardFocus:   false
IsKeyboardFocusable:    true
IsEnabled:  true
ClassName:  "C:\Program Files (x86)\RedactedProgramFiles7\RedactedClientFolder"
HelpText:   ""
IsPassword: false
NativeWindowHandle: 0x270670
IsOffscreen:    false
FrameworkId:    "Win32"
ProviderDescription:    "[pid:4000,hwnd:0x270670 Main:Nested [pid:8160,hwnd:0x270670 Annotation(parent link):Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main:Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]; Nonclient:Microsoft: Non-Client Proxy (unmanaged:uiautomationcore.dll); Hwnd(parent link):Microsoft: HWND Proxy (unmanaged:uiautomationcore.dll)]"
Window.CanMaximize: true
Window.CanMinimize: true
Window.WindowVisualState:   Normal (0)
Window.WindowInteractionState:  ReadyForUserInteraction (2)
Window.IsModal: false
Window.IsTopmost:   false
Transform.CanMove:  true
Transform.CanResize:    true
Transform.CanRotate:    false
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    ""
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: ""
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "Redacted"
LegacyIAccessible.Role: client (0xA)
LegacyIAccessible.State:    focusable (0x100000)
LegacyIAccessible.Value:    ""
IsDockPatternAvailable: false
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsRangeValuePatternAvailable:   false
IsScrollPatternAvailable:   false
IsScrollItemPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsTablePatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTextPatternAvailable: false
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    true
IsValuePatternAvailable:    false
IsWindowPatternAvailable:   true
IsItemContainerPatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsSynchronizedInputPatternAvailable:    false
FirstChild: "Workspace" pane
LastChild:  "Application" menu bar
Next:   "Inspect  (HWND: 0x01700F06)" window
Previous:   "Sandbox Console (Debugging) - Microsoft Visual Studio (Administrator)" window
Other Props:    Object has no additional properties
Children:   "Workspace" pane
    (null) title bar
    "Application" menu bar
Ancestors:  "Desktop" pane
    [ No Parent ]

Here are the properties of the problem "Workspace" pane.

How found:  Selected from tree...
RuntimeId:  "[42.34146524]"
BoundingRectangle:  {l:83 t:51 r:1303 b:995}
ProcessId:  8160
ControlType:    UIA_PaneControlTypeId (0xC371)
LocalizedControlType:   "pane"
Name:   "Workspace"
AccessKey:  ""
HasKeyboardFocus:   false
IsKeyboardFocusable:    true
IsEnabled:  true
ClassName:  "MDIClient"
HelpText:   ""
IsPassword: false
NativeWindowHandle: 0x20908DC
IsOffscreen:    false
FrameworkId:    "Win32"
ProviderDescription:    "[pid:4000,hwnd:0x20908DC Main:Nested [pid:8160,hwnd:0x20908DC Annotation(parent link):Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main:Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]; Hwnd(parent link):Microsoft: HWND Proxy (unmanaged:uiautomationcore.dll)]"
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    ""
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: ""
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "Workspace"
LegacyIAccessible.Role: client (0xA)
LegacyIAccessible.State:    focusable (0x100000)
LegacyIAccessible.Value:    ""
IsDockPatternAvailable: false
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsRangeValuePatternAvailable:   false
IsScrollPatternAvailable:   false
IsScrollItemPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsTablePatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTextPatternAvailable: false
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    false
IsValuePatternAvailable:    false
IsWindowPatternAvailable:   false
IsItemContainerPatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsSynchronizedInputPatternAvailable:    false
FirstChild: "Untitled3" window
LastChild:  "Letters (32638 of 32638):" window
Next:   (null) title bar
Previous:   [null]
Other Props:    Object has no additional properties
Children:   "Untitled3" window
    "Letters (32638 of 32638):" window
Ancestors:  "Redacted" window
    "Desktop" pane
    [ No Parent ]

Here are the properties of the "Working" document window.

How found:  Selected from tree...
RuntimeId:  "[42.9505096]"
BoundingRectangle:  {l:85 t:53 r:651 b:491}
ProcessId:  8160
ControlType:    UIA_WindowControlTypeId (0xC370)
LocalizedControlType:   "window"
Name:   "Untitled3"
AccessKey:  ""
HasKeyboardFocus:   false
IsKeyboardFocusable:    true
IsEnabled:  true
AutomationId:   "10"
ClassName:  "ProToolsSubMDIWndClass"
HelpText:   ""
IsPassword: false
NativeWindowHandle: 0x910948
IsOffscreen:    false
FrameworkId:    "Win32"
ProviderDescription:    "[pid:4000,hwnd:0x910948 Main:Nested [pid:8160,hwnd:0x910948 Annotation(parent link):Microsoft: Annotation Proxy (unmanaged:uiautomationcore.dll); Main:Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]; Nonclient:Microsoft: Non-Client Proxy (unmanaged:uiautomationcore.dll); Hwnd(parent link):Microsoft: HWND Proxy (unmanaged:uiautomationcore.dll)]"
Window.CanMaximize: true
Window.CanMinimize: true
Window.WindowVisualState:   Normal (0)
Window.WindowInteractionState:  ReadyForUserInteraction (2)
Window.IsModal: false
Window.IsTopmost:   false
Transform.CanMove:  true
Transform.CanResize:    true
Transform.CanRotate:    false
LegacyIAccessible.ChildId:  0
LegacyIAccessible.DefaultAction:    ""
LegacyIAccessible.Description:  ""
LegacyIAccessible.Help: ""
LegacyIAccessible.KeyboardShortcut: ""
LegacyIAccessible.Name: "Untitled3"
LegacyIAccessible.Role: client (0xA)
LegacyIAccessible.State:    focusable (0x100000)
LegacyIAccessible.Value:    ""
IsDockPatternAvailable: false
IsExpandCollapsePatternAvailable:   false
IsGridItemPatternAvailable: false
IsGridPatternAvailable: false
IsInvokePatternAvailable:   false
IsLegacyIAccessiblePatternAvailable:    true
IsMultipleViewPatternAvailable: false
IsRangeValuePatternAvailable:   false
IsScrollPatternAvailable:   false
IsScrollItemPatternAvailable:   false
IsSelectionItemPatternAvailable:    false
IsSelectionPatternAvailable:    false
IsTablePatternAvailable:    false
IsTableItemPatternAvailable:    false
IsTextPatternAvailable: false
IsTogglePatternAvailable:   false
IsTransformPatternAvailable:    true
IsValuePatternAvailable:    false
IsWindowPatternAvailable:   true
IsItemContainerPatternAvailable:    false
IsVirtualizedItemPatternAvailable:  false
IsSynchronizedInputPatternAvailable:    false
FirstChild: "" thumb
LastChild:  (null) title bar
Next:   "Letters (32638 of 32638):" window
Previous:   [null]
Other Props:    Object has no additional properties
Children:   "" thumb
    (null) title bar
Ancestors:  "Workspace" pane
    "Redacted" window
    "Desktop" pane
    [ No Parent ]

解决方案

Very nice question. Based upon the problem you've documented, it's clear that your conditional:

PropertyCondition workspaceCond = new PropertyCondition(
 AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);

... fails evaluation. Why?

The answer is how your conditional is evaluated. Looking at your element tree, we notice this property for Workspace:

IsWindowPatternAvailable:   false

And for the main window and Untitled3:

IsWindowPatternAvailable:   true

From MSDN:

UIA_IsWindowPatternAvailablePropertyId 30044

Identifies the IsWindowPatternAvailable property, which indicates whether the Window control pattern is available for the automation element. If TRUE, a client can retrieve an IUIAutomationWindowPattern interface from the element.

We find a repro in this thread, which implies the same failure pattern as the one you are currently experiencing. We also note the lack of Window properties present for this element because IUIAutomationWindowPattern is inaccessible.

A workaround is available from the aforelinked thread. Instead of PropertyCondition, one might use:

public class ConditionMatcher : IMatchConditions
{
    public bool Matches(AutomationElement element, Condition condition)
    {
        return new TreeWalker(condition).Normalize(element) != null;
    }
}

Or, alternately, one might use the workaround you've given, provided your tree structure is guaranteed to be shallow (and thus, appropriate to the name of this site, will not trigger a stack overflow).

Admittedly, this wasn't the most obvious issue. In the perfect world, MSDN should have better documentation on this topic.

这篇关于AutomationElement显示了使用Inspect.exe但使用UIAutomationCore.dll或System.Windows.Automation时,确实显示不起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆