XAML Spy for Windows

XAML Spy 3

XAML Spy supports all major XAML desktop and mobile platforms on Windows, including Silverlight, WPF, Windows Phone, and Windows Store. For detailed information on the supported platforms and versions see the support matrix in the platform support page.

Client library

In order to spy a Windows app, a small XAML Spy client library needs to be referenced by the app. The client library includes a communication protocol for connectivity with the XAML Spy Windows service, and exposes a set of services that enables querying the app for a wide variety of data. Among others this includes read/write access to the XAML visual tree, and access to the full property set of any UIElement that is located in the visual tree.

How it works

To be discoverable by XAML Spy, the client library needs to connect to the XAML Spy Windows service on start of the app. In order to deliver a smooth and frictionless experience, setting up the connection is completely automated. Other than referencing the client library, there is no need to manually alter the app to enable XAML Spy.

The XAML Spy client library is distributed using the NuGet package XamlSpy. The NuGet package installs a MSBuild targets file, which automatically adds a reference to the XAML Spy client library when a debug build occurs. The targets file includes a task that pre-processes each XAML file found in the project. The XAML files are decorated with metadata that bootstraps XAML Spy, and additional information such as source code line info is added to the XAML. It is important to understand that XAML Spy does not modify the source XAML files. It does create temporary copies of the XAML files in the obj folder of your project, and redirects the XAML compiler to use these files instead of the original XAML files.

Next to decorating the XAML source files, XAML Spy adds an assembly attribute PoweredByXamlSpy to the app. The use of this attribute ensures that a hard reference is created from the app to the client library.

Debug builds

XAML Spy is configured to enable itself only when an app is build in debug mode. When building an app in release mode, XAML Spy is not enabled. Spying an app is typically used in a debug scenario, you would not want to deploy your app with XAML Spy enabled. Next to that, submitting an app to the Windows store with XAML Spy included does violate the store policies, and will cause your app submission to be rejected.

The build output window in Visual Studio logs the XAML Spy build activities. The following shows the typical output from building an UWP project in debug mode when the XAML Spy NuGet package is referenced. Each decorated XAML file is listed in the output.

1>------ Rebuild All started: Project: App42, Configuration: Debug x86 ------
1>  Decorating App.xaml
1>  Decorating MainPage.xaml
1>  App42 -> C:\Projects\Lab\App42\App42\bin\x86\Debug\App42.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

When building in release mode, a message is shown that XAML Spy is not enabled.

1>------ Rebuild All started: Project: App42, Configuration: Release x86 ------
1>  XAML Spy is not enabled
1>  App42 -> C:\Projects\Lab\App42\App42\bin\x86\Debug\App42.exe
1>  Starting .NET Native compilation
...
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

Manually add XAML Spy

Enabling XAML Spy by simply referencing a NuGet package covers most scenarios. For maximum flexibility, it is also possible to manually reference and bootstrap the XAML Spy client library. An in-depth explanation on how to achieve that can be found in the How to connect manually tutorial.