{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

WP8 XAML and DirectX App Crashes When Adding Navigation

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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks