如何访问未实现AutomationPeer类的WPF自定义控件的内部元素? [英] How to access inner elements of a WPF custom control that doesn't implement the AutomationPeer class?

查看:147
本文介绍了如何访问未实现AutomationPeer类的WPF自定义控件的内部元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我从这篇文章中得知–

http://blogs.msdn.com/b/patrickdanino/archive/2009/11/11/custom-controls-and-ui-automation.aspx

-WPF中的控件负责自己公开其UIA项,并且自定义控件的任何新添加的功能对于UIA都是不可用的,直到它们通过相应的AutomationPeer类的实现公开为止.在我的工作中,我被分配给使用大量工具栏的WPF应用程序的UI测试自动化.问题是,通过Microsoft UI自动化库,我可以作为AutomationElements访问工具栏(显然是作为自定义控件开发的),但无法访问其中的按钮-子代数/后代集合始终返回0.使用编码的UI测试时,测试始终失败,并显示以下错误消息:

测试方法CAM2QDummyTest.CodedUITest2.CodedUITestMethod1引发异常:

Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnBlockedControlException:另一个控件正在阻止该控件.请使被阻止的控件可见,然后重试该操作.

其他详细信息:

技术名称:"MSAA"

名称:``标准''

ControlType:"工具栏"

---> System.Runtime.InteropServices.COMException:来自HRESULT的异常:0xF004F003


显然他们没有实现相应的AutomationPeer类.现在,我只有应用程序,而没有源代码.因此,我无法以上面提到的文章中描述的方式解决问题.任何人都可以通过任何线索来帮助我如何访问工具栏的内部按钮?任何建议将不胜感激.感谢所有人.

Recently I came to know from the article –

http://blogs.msdn.com/b/patrickdanino/archive/2009/11/11/custom-controls-and-ui-automation.aspx

- that controls in WPF are responsible for exposing their UIA items themselves, and any newly added fuctionalities of a custom control aren’t available to the UIA until they are exposed through the implementation of the corresponding AutomationPeer class. At my work I have been assigned to the automation of UI testing of a WPF application that employs a large number of ToolBars. The problem is, through Microsoft UI Automation Library I can access the ToolBars (apparently which are developed as custome control) as AutomationElements, but I cannot access the Buttons inside them – Count of Children/Descendant Collection always return 0. When using Coded UI Test, the tests always fail and shows the following Error Message:

Test method CAM2QDummyTest.CodedUITest2.CodedUITestMethod1 threw exception:

Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnBlockedControlException: Another control is blocking the control. Please make the blocked control visible and retry the action.

Additional Details:

TechnologyName: ''MSAA''

Name: ''Standard''

ControlType: ''ToolBar''

---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0xF004F003


Apparently they didn’t implement the corresponding AutomationPeer classes. Now, I only have the application, not the source code. So I cannot solve the problem in the way described in the article I mentioned above. Can anyone HELP with any clue how can I get access to the inner Buttons of the ToolBars? Any suggestion will be greatfully appreciated. Thanks to all.

推荐答案

我不确定我是否完全理解您的问题,但是也许您可以使用VisualTreeHelper?

链接: http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper.aspx [ ^ ]

另一方面,如果这些控件仅通过自定义渲染/绘画(根本不使用任何元素)实现伪内部控件,那么您可能就不走运了.

[根据评论通过讨论进行更新]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~

好吧,我更好地了解了您的问题.我以前的假设是您正在从普通应用程序访问它,但是那个假设是错误的.

考虑到您无权访问源代码,我真的看不到如何解决此问题.也许您可以将这些信息传递回给开发人员,并要求他们正确实施这些信息,以便使它发挥自己的作用?

另一个值得尝试的想法是从该控件派生一个类,并在那里处理自动化事件,然后测试此派生类.派生类的唯一目的是添加缺少的自动化内容.可能不起作用,但值得一试.
I am not sure I fully understood your question, but perhaps you can use VisualTreeHelper?

Link: http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper.aspx[^]

On the other hand, if these controls implement the pseudo-inner controls purely via custom rendering/painting (without using any elements at all), then you may be out of luck.

[Update based on our discussion via comments]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alright, I understand your problem better. My previous assumption was that you were accessing this from a normal application, but that assumption was wrong.

Considering you don''t have access to the source code I don''t really see how you can work around this. Perhaps you can pass this information back to the developers and ask them to implement this properly so you can get it working your end?

Another idea worth trying is to derive a class from this control, and handle the automation event there, and then test this derived class. The only purpose of the derived class would be to add the missing automation stuff. May not work, but worth a shot.


处理该控件的Loaded事件,并查看此时是否填充了它的Children大学生. WPF控件在呈现之前没有任何子级.
Handle the control''s Loaded event, and see if its Children colleciton is populated at that point. A WPF control doesn''t have any children until it has been rendered.


这篇关于如何访问未实现AutomationPeer类的WPF自定义控件的内部元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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