ofe*_*fer 65 .net multithreading dispatcher windows-8 windows-runtime
如何在WinRT(Windows 8 Metro)中的UI线程上运行代码?
该Invoke方法不存在.
Cœu*_*œur 79
从非UI线程直接获取CoreWindow更容易.以下代码可以在任何地方使用,即使是GetForCurrentThread()或Window.Current返回null.
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
<lambda for your code which should run on the UI thread>);
Run Code Online (Sandbox Code Playgroud)
例如:
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
// Your UI update code goes here!
});
Run Code Online (Sandbox Code Playgroud)
您需要引用Windows.ApplicationModel.Core命名空间:
using Windows.ApplicationModel.Core;
Run Code Online (Sandbox Code Playgroud)
Rei*_*man 69
使用:
从您的UI线程,执行:
var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
Run Code Online (Sandbox Code Playgroud)
从你的背景(非UI线程)
dispatcher.RunAsync(DispatcherPriority.Normal,
<lambda for your code which should run on the UI thread>);
Run Code Online (Sandbox Code Playgroud)
这应该适用于CP和后期版本.
小智 8
使用:
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Frame.Navigate(typeof(Welcome), this));
Run Code Online (Sandbox Code Playgroud)
这个对我有用.
在我看来,这是一种更容易的方法.
获取与UI关联的TaskScheduler.
var UISyncContext = TaskScheduler.FromCurrentSynchronizationContext();
Run Code Online (Sandbox Code Playgroud)
然后在上面的UISyncContext上启动一个新任务.
Task.Factory.StartNew(() => { /* Do your UI stuff here; */}, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, UISyncContext);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58796 次 |
| 最近记录: |