In the end, I created a base popup class and added this code to:
private void HookIntoPage() { if (UserControlHelper.RootVisual != null) { PhoneApplicationPage page = (PhoneApplicationPage)UserControlHelper.RootVisual.Content; // Hook up into the back key press event of the current page page.BackKeyPress += BackKeyPress; FrameworkElement element = page.FindVisualChild("MainGrid"); if (element != null && element is Grid) { Grid grid = element as Grid; grid.Children.Add(this); } else { throw new Exception("Popup cannot find MainGrid"); } } }
It is slightly hardcoded as it searches for the MainGrid grid control. This can be improved to find the appropriate top-level container.
There are 2 helper classes here. UserControlHelper has the following methods:
public static PhoneApplicationFrame RootVisual { get { return Application.Current == null ? null : Application.Current.RootVisual as PhoneApplicationFrame; } }
FindVisualChild comes from the VisualTreeHelperExtension class that comes with Phone7.Fx.preview, which I use for my application panel.
source share