的方式来获得在Windows商店应用的CoreDispatche

的方式来获得在Windows商店应用的CoreDispatche

本文介绍了正确的方式来获得在Windows商店应用的CoreDispatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Windows应用商店的应用程序,我有一些code需要被发布到UI线程。

对于这一点,我想找回CoreDispatcher并用它来发布code。

似乎有几个方法可以做到这一点:

  //第一种方式
Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()CoreWindow.Dispatcher。//方式二
Window.Current.Dispatcher;

我不知道哪一个是正确的?或者,如果两者是等价的?


解决方案

这是preferred方式:

<$p$p><$c$c>Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
()=&GT;
{
    //你的UI更新code到这里!
});

这样做的好处是,它得到主 CoreApplicationView 等始终可用。更多细节.

有两种选择,你可以使用。

第一种选择

  Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()。CoreWindow.Dispatcher

这将获取应用程序的活动视图,但是这会给你的的,如果没有意见已被激活。更多细节.

第二个选择

  Window.Current.Dispatcher

这是行不通时,它从另一个线程调用,因为它返回的的而不是 UI调度的。更多细节.

I'm building a Windows Store app, and I have some code that needs to be posted to the UI thread.

For that, i'd like to retrieve the CoreDispatcher and use it to post the code.

It seems that there are a few ways to do so:

// First way
Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher;

// Second way
Window.Current.Dispatcher;

I wonder which one is correct? or if both are equivalent?

解决方案

This is the preferred way:

Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    // Your UI update code goes here!
});

The advantage this has is that it gets the main CoreApplicationView and so is always available. More details here.

There are two alternatives which you could use.

First alternative

Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher

This gets the active view for the app, but this will give you null, if no views has been activated. More details here.

Second alternative

Window.Current.Dispatcher

This is will not work when it's called from another thread as it returns null instead of the UI Dispatcher. More details here.

这篇关于正确的方式来获得在Windows商店应用的CoreDispatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:31