Over a million developers have joined DZone.

WP8 XAML and DirectX App Crashes When Adding Navigation

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

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.

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Toni Petrina , DZone MVB .

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}