在工作线程中遍历 VisualTree 以进行全文搜索
我正在使用 WPF 来显示复杂的数据(想想报告)。我现在需要对其进行全文搜索。目前,我们通过遍历视觉树寻找文本块来做到这一点。看来这需要在UI线程上完成,是吗?这里的问题是,可能需要相当长的时间才能找到下一个匹配项,在此期间整个 UI 都会被阻止。
有办法规避这个问题吗?我可以尝试使用 UI 自动化,但一旦找到匹配项,如何突出显示它?使用 UI 自动化,我无法获取对 UI 元素的实际对象实例的引用,因此我无法在其上放置装饰器。
WPF中的全文搜索通常是如何实现的? XPS 查看器是如何做到这一点的?
I am using WPF to show complex data (think reporting). I now need to have a fulltext search for it. We currently do this by walking the visual tree looking for textblocks. It seems that this needs to be done on the UI thread, is that right? The problem here is that it might take quite a while until the next match is found during wich the whole UI blocks.
Is there a way to circumvent this problem? I could try to use UI Automation but once I found a match how do I highlight it? Using UI Automation I do not get references to the actual object instance of the UI Element, hence I can not put an adorner on top of it.
How is fulltext search in WPF usually implemented? How does the XPS viewer do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是正确的,查询可视化树必须在 UI 线程上完成,而且 UI 自动化只能以与坐在计算机前的人类似的方式与应用程序交互,因此它无权访问实际控制(就像人没有控制一样)。
解决此问题的正确方法是将 UI 放在一边并在数据源(MVVM 中的模型)中搜索文本。毕竟,您真正想要搜索的是 UI 显示的信息,而不是 UI 本身。
You are correct in that querying the visual tree will have to be done on the UI thread, and also that UI Automation can only interact with an application in a similar way to a person sitting at the computer, so it doesn't have access to the actual controls (just as the person doesn't).
The correct way to approach this is to put the UI aside and search for text in your data source (the model in MVVM) instead. After all, you really want to search the information displayed by your UI and not the UI itself.
wpf 自动将 inotifypropertychanged 编组到 ui 线程上,以便您可以在另一个线程上进行处理并设置属性。如何执行此操作取决于代码的结构。例如,您使用 mvvm 吗?如果没有,您可能需要使用调度程序来调用用户界面上的任何方法。如果您回复有关您的代码结构的更多信息,我应该能够提供更明确的答案。如果您不使用 mvvm,如果您将 wpf 数据绑定与可以进行处理的非 ui 数据源一起使用,您仍然可以避免显式封送。
wpf automatically marshals inotifypropertychanged onto the ui thread so you can do your processing on another thread and set properties. how you do this depends on the structure of your code. for example are you using mvvm? if not you may meed to use the dispatcher to invoke any methods on your ui. if you reply with a bit more info about you code structure i should be able to provide a more difinitive answer. if you are not using mvvm you can still avoid explicitly marshaling if you use wpf databinding with a non ui datasource that you can do your processing on.