Well, you're initializing the CoreDispatcher
based on whichever thread happens to initialize the class. Given that you really want it to be done for a specific dispatcher, this feels like it's a bad idea to me.
Given that you're using this from a page, can't you use the dispatcher for that page instead? I don't see why you need a dispatcher
variable at all... you might consider writing an extension method for OnUiThread
instead.
Also, you don't really need to use async
/await
here - or a lambda expression. You can just use:
public static Task OnUiThread(this CoreDispatcher dispatcher, Action action)
{
return dispatcher.RunAsync(CoreDispatcherPriority.Normal,
new DispatchedHandler(action))
.AsTask();
}