在 WinRT 中的 UI 线程上运行代码 [英] Run code on UI thread in WinRT

查看:35
本文介绍了在 WinRT 中的 UI 线程上运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 WinRT (Windows 8 Metro) 的 UI 线程上运行代码?

How can I run code on the UI thread in WinRT (Windows 8 Metro)?

Invoke 方法不存在.

推荐答案

从非 UI 线程直接获取 CoreWindow 更容易.以下代码适用于任何地方,即使 GetForCurrentThread()Window.Current 返回 null.

It's easier to directly get the CoreWindow from the non-UI thread. The following code will work everywhere, even when GetForCurrentThread() or Window.Current returns null.

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    <lambda for your code which should run on the UI thread>);

例如:

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

您需要引用 Windows.ApplicationModel.Core 命名空间:

You'll need to reference Windows.ApplicationModel.Core namespace:

using Windows.ApplicationModel.Core;

这篇关于在 WinRT 中的 UI 线程上运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆