How to find your XAML Page object in the debugger


Taking a quick break from compiler performance to give a short debugging tip. If you're debugging a managed application that is based on the typical frame navigation that the built-in templates and samples use and would like to find your the page that is currently being displayed, you can simply paste this into your Watch window.

((Windows.UI.Xaml.Controls.Frame)Window.Current.Content).Content

Breaking it down, here is what this does.

  1. From the Window class, we access the Current property. This will get the currently activated window for an application. The property is static, so we don't need an instance - that's why you can use this regardless of what method your debugger session is broken into.
  2. From that instance, we access the Content property. This is the visual root of an application window, which is always some form of UIElement. If you are using the built-in templates, this will be a Frame, so we cast the content to that type.
  3. From that Frame instance, we access the Content property. This is the second ".Content" in the expression above. This can be any object, but again if you are using frames and pages to structure your app, it will be the Page you are looking for. We don't cast this to anything because the debugger can figure out the type of the instance on its own and will show the right properties.

Typically the content page will be something like your app's MainPage instance from MainPage.xaml (or whatever content you currently have active). From there, you can drill into named controls for example and take a look at the state of what is being displayed.

Enjoy!

Comments (2)

  1. Felipe Romero says:

    Thank you, this will come in handy.

Skip to main content