What technique does Snoop uses to inspect a WPF application

前端 未结 4 1886
一向
一向 2021-02-06 01:15

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

4条回答
  •  我寻月下人不归
    2021-02-06 01:55

    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

    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.

    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;
    

提交回复
热议问题