Snoop 使用什么技术来检查 WPF 应用程序 [英] What technique does Snoop uses to inspect a WPF application

查看:36
本文介绍了Snoop 使用什么技术来检查 WPF 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Snoop 是间谍实用程序,它使用一些强大的技术(可能是某种反射)来检查正在运行的 WPF 应用程序.最有趣的是,Snnop 能够读出整个对象结构.

Snoop, the spy utility, uses some powerful technique (probably some sort of reflection) to inspect a running WPF application. Most interesting is the fact, that Snnop is able to readout the entire object structure.

前几天下载了Snoop源码,花了一些时间研究内部行为.不幸的是,我目前还无法了解 Snoop 是如何做这些事情的,所以我希望任何人都可以帮助我.

A few days ago I downloaded the Snoop source code and spent some time on studying the internal behavior. Unfortunately, I couldn't find out yet how Snoop is doing these things, so I hope that anybody can help me out.

在工作中,我目前正在编写一个编码的 UI 测试框架,如果我能够访问应用程序的对象结构,那就太棒了,因为这不仅能让我断言 UI 状态.

At work I am currently writing a Coded UI Testing-Framework and it would be fantastic if I had access to the application's object structures because this would allow me to not only assert the UI state.

更新:

这是需要的代码:

string filePath = "WpfApp.exe";
AppDomain appDomain = AppDomain.CurrentDomain;
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
Assembly ass = appDomain.Load(bytes);
ass.EntryPoint.Invoke(null, new object[] { });
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
Window w = System.Windows.Interop.HwndSource.FromHwnd(handle).RootVisual as Window;

这对我来说已经是一个很大的帮助,但了解 Snoop 如何将自己注入另一个进程也很有趣.

This is already a big help for me, but it is also interesting to find out, how Snoop injects itself into another process.

推荐答案

您可以使用 WPF VisualTreeHelper 和/或 LogicalTreeHelper 来完成 Snoop 所做的事情.一旦你掌握了任何视觉元素,你几乎可以遍历它的整个视觉树来查看它包含的所有元素.可视化树助手这里

You can accomplish what Snoop does by using the WPF VisualTreeHelper and/or the LogicalTreeHelper. Once you get a hold of any visual element, you can pretty much traverse its entire visual tree to see all the elements it contains. Visual tree helper here

因此,在您的 UI 测试中,抓取主窗口并遍历其可视化树以找到您想要的任何元素,然后对该元素执行您想要的任何验证或操作.

So in your UI test, grab the main window and traverse its visual tree to find any element you want and then perform any validations or operations you want on that element.

此外,您可以使用 System.Diagnostics.Process.MainWindowHandle 从现有进程获取窗口句柄,然后使用窗口句柄创建 wpf 窗口.已经有一段时间了,所以如果没有做更多研究,我就不记得具体细节了.下面的代码可能会有所帮助:

Furthermore, you may be able to use System.Diagnostics.Process.MainWindowHandle to get the windows handle from an existing process and then use the window's handle to create a wpf window. Its been a while so I dont remember the specifics without doing more research. The code below may help:

Window window = (Window)System.Windows.Interop.HwndSource.FromHwnd(process.MainWindowHandle).RootVisual;

这篇关于Snoop 使用什么技术来检查 WPF 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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