问题
I have some WP7 code that looks like this:
using System.Windows;
using System.Windows.Threading;
public static class GlobalDispatcher
{
public static Dispatcher Current { get { return Deployment.Current.Dispatcher; } }
}
What is the equivalent in WinRT? Is there no globally accessible Dispatcher (or CoreDispatcher) object?
回答1:
You will want to use Window.Current.Dispatcher
.
回答2:
Just adding this as answer, as it better solves the problem in my opinion:
Window.Current is null if the application is not focused and thus will bring you in trouble when there is a user involved ;)
CoreApplication.MainView.CoreWindow.Dispatcher isn't null...
– Jeremy White Aug 4 '13 at 13:09
回答3:
try this:
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
try
{
//Your code here
}
catch (Exception ex)
{
HandleException(ex);
}
});
来源:https://stackoverflow.com/questions/13482205/global-dispatcher-in-winrt