Mobile Zone is brought to you in partnership with:

My name is Toni Petrina and I am a software developer and an occasional speaker. Although I primarily develop on the Microsoft stack, I like to learn new technologies. My hobbyist projects range from game development, regardless of the technology, to ALM. I spend most of my time with my girlfriend and someday I will learn how to play the guitar properly. Toni is a DZone MVB and is not an employee of DZone and has posted 69 posts at DZone. You can read more from them at their website. View Full User Profile

WP8 XAML and DirectX App Crashes When Adding Navigation

  • submit to reddit

Creating applications from default templates without understanding them might introduce serious bugs in your application. Consider the simple Windows Phone XAML and Direc3D App project template. Let’s create basic app and pretend we are building a new game. We probably want to add some other XAML pages e.g. for settings, high scores, about dialog, etc.

I will first add simple button with caption “High scores” to the bottom of MainPage using the following XAML:

<Button Content="High scores"
        Click="_btnHighScores_Click" />

The application looks simple enough so far.

Let’s add a new page to the project named HighScoresPage and let’s implement the click event handler for the above created button. We will simply navigate to the high scores page from the handler.

private void _btnHighScores_Click(object sender, RoutedEventArgs e)
    NavigationService.Navigate(new Uri("/HighScoresPage.xaml", UriKind.Relative));

Our high scores page is beautifully designed with snarky comment:

Once you hit that back button, the application will crash. This is due to the relatively superficial sample code provided with the sample, but it can destroy your nerves and it simply shouldn’t be this way. What happens is that DrawingSurface is loaded twice and also requests the drawing provider twice. After the second provider connects to the surface, the first one disconnects but in doing so, it removes the second provider also!

The fix is simple, add the following field to the class:

private object m_d3dContentProvider = null;

And also change the way the content provider is created and bound:

if (m_d3dContentProvider == null)
    m_d3dContentProvider = m_d3dInterop.CreateContentProvider();

Now it works the way it's supposed to.

Published at DZone with permission of Toni Petrina, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)