Global Dispatcher in WinRT?

帅比萌擦擦* 提交于 2019-12-10 13:55:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!