Search code examples
c#windows-runtimewindows-phone-8.1windows-store-appswin-universal-app

Is it necessary to dispatch every call to Navigate?


I navigate through my Universal Windows 8.1/Windows Phone 8.1 app using:

((Frame)Window.Current.Content).Navigate()

There were some crashes reported by users when navigating. It was especially serious with a thrid party schedule component which did nearly crash every time someone navigated from it (by clicking an appointment) to another page.

The vendor told us too wrap our navigation in a dispatcher.

Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { `((Frame)Window.Current.Content).Navigate() });`

This did alleviate the errors regarding the schedule control completely. It's still to early to see if the navigation has improved in every situation by this. It has the serious drawback that people can still interact with elements for a short time until navigation occurs now.

Is this a best practice at all, really neccessary or is this a special corner case?

Further the crash did still happened when we tried to use HasThreadAccess to limit the dispatched calls to the ones it should be neccessary.


Solution

  • By default all XAML and their code-behinds are owned by the main thread (UI thread). In addition, any objects created on the main thread do not need to be dispatched. The problem you are seeing is most likely caused by a background thread/task that tries to call to the UI thread for navigation.