Navigating to XAML and showing keyboard chrashes the game

I’m trying to navigate to a XAML page from WP Monogame app. Everything works well, but when on-screen keyboard is shown (after touching a textbox) it crashes with the following exception:

{System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Controls.SipHelper.AdjustFrame(FrameworkElement element, Double bottomMargin, Double& stillObscured)
   at System.Windows.Controls.SipHelper.FlickIn(FrameworkElement element, Double bottomMargin)
   at Microsoft.Phone.Controls.PhoneApplicationPage.SipAndNavBarChange()
   at Microsoft.Phone.Controls.PhoneApplicationPage.set_VisibleRegionInPhysicalPixels(RECT value)
   at Microsoft.Phone.Controls.PhoneApplicationPage.UpdateCurrentVisualState()
   at Microsoft.Phone.Controls.PhoneApplicationPage.HandleVisibleRegionChange()
   at Microsoft.Phone.Controls.PhoneApplicationPage.OnVisibleRegionChange()
   at Microsoft.Phone.TaskModel.Interop.Task.FireOnVisibleRegionChange()}

I’m navigating to XAML page as follows:

MainPage.xaml contains the following parameters and function:

    UIElement savedVisual;

private TestPage ap; // This is the XAML form with some text and TextBox field
        public void ShowXamlForm()
            savedVisual = App.Current.RootVisual;
            ap = new TestPage();
            App.Current.RootVisual = ap;

Actual Game class contains this call:
Deployment.Current.Dispatcher.BeginInvoke(() => mainPage.ShowXamlForm());

mainPage variable is referring to the MainPage object.

I’m on a really thin ice here, took some code snippets here and there and I’m probably doing something wrong. Does anyone have any ideas of how to avoid crashing?

The reason is here :

“You can set the value of the RootVisual property only one time from code, although you get its value any number of times.”

This property is set by default when your app starts, so you can’t change it after that.


// Do not add any additional code to this method
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
    // Set the root visual to allow the application to render
    if (RootVisual != RootFrame)
        RootVisual = RootFrame;

    // Remove this handler since it is no longer needed
    RootFrame.Navigated -= CompleteInitializePhoneApplication;