Win RT equivalent for Deployment.Current.Dispatcher.BeginInvoke

1 1 1 1 1 1 1 1 1 1 Rating 0.00 (0 Votes)

Recently while working on my first Windows Phone 8.1 RT app, I came across situation which required to call MessageDialog from the background thread to prevent the UnauthorizedAccessException. Having worked previously on Windows Phone 8 Silverlight apps, I was looking for an equivalent to Deployment.Current.Dispatcher.BeginInvoke

In SilverLight:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
   //UI related stuff
});

 While I found the answer to be mentioned as below when I searched over the internet:

Windows.UI.Core.CoreWindow.GetForCurrentThread().
	Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
	{
		// UI Stuff goes here
	});

However, the GetForCurrentThread() was returning null and causing a NullPointer exception.
After some more research, I found that the below code works well for all scenarios.

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
    {
       // UI Stuff goes here
     });

 
Hope this small article helps in resolving this issue, if you come across the same.


Joomla SEO by MijoSEF