I'm working on a universal app to run on Windows 8.1 & Windows Phone 8.1. In the older Silverlight based Windows Phone apps, I could have a handy helper on my View Model something like:
protected delegate void OnUIThreadDelegate();
protected static void OnUIThread(OnUIThreadDelegate onUIThreadDelegate)
{
if (onUIThreadDelegate != null)
{
if (Deployment.Current.Dispatcher.CheckAccess())
{
onUIThreadDelegate();
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(onUIThreadDelegate);
}
}
}
From what I've seen of using async
I should be able to do something like:
async void Watcher_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
this.Latitude = args.Position.Coordinate.Point.Position.Latitude;
this.Longitude = args.Position.Coordinate.Point.Position.Longitude;
// TODO: Consider reloading;
var ev = await SpecialEvent.SearchNearAsync();
this.Events.Clear(); // this is an ObservableCollection that the XAML is bound to,
// and I'm seeing it blow up with RPC_E_WRONG_THREAD here
}
Other options utilising the Dispatcher
seem to be harder, as, in universal apps, Application.Current
does not have a Dispatcher
property?
So what are the options left? do I have to change my View-Models to allow the dispatcher to be passed from the View, or am I missing something more obvious?