Gaz83
Gaz83

Reputation: 2875

UWP navigation causing Access Viloation

I am converting my WP 8.1 app to UWP and as I was interested in using prism I thought I would build my app from the ground up. I have created a page with a button the navigates to another page, deployed the app and gave it a go. When I click the button to navigate the following code is executed

private void Button1_Click(object sender, RoutedEventArgs e)
{
    this.Frame.Navigate(typeof(TestPage));
}

which then crashes the app. When I look at the output window in VS2015 I see the following at the bottom.

The program '[4760] MyApp.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Both pages have ViewModels and are set using Prisms ViewModelLocator.AutoWireViewModel. I tried clearing all the properties of the ViewModels so they are empty with no code but that did nothing. Put a breakpoint in the view model constructor and it does reach this point when debugging.

Any ideas what this means or where else to look?

UPDATE: Issue looks like it is caused by a control template. I narrowed the issue down to a button control by commenting out all the XAML and then adding things back one by one. I then found the control causing the issue so removed the binding and style template. Added the binding back and all was ok, added the template back and the issue occurred.

Upvotes: 3

Views: 840

Answers (1)

Christian Bekker
Christian Bekker

Reputation: 1867

You might need to use a dispatcher to execute the code on the correct thread.

//Initiate and set this at the startup of your app, on the UI thread.
dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

Then use the dispatcher to run the code:

private async void Button1_Click(object sender, RoutedEventArgs e)
{
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            this.Frame.Navigate(typeof(TestPage));
}

Upvotes: 2

Related Questions