3 回答

TA貢獻1842條經驗 獲得超13個贊
從非UI線程直接獲取CoreWindow更容易。以下代碼可以在任何地方使用,即使是GetForCurrentThread()或Window.Current返回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命名空間:
using Windows.ApplicationModel.Core;

TA貢獻1890條經驗 獲得超9個贊
使用:
從您的UI線程,執(zhí)行:
var dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
從你的背景(非UI線程)
dispatcher.RunAsync(DispatcherPriority.Normal,
<lambda for your code which should run on the UI thread>);
這應該適用于CP和后期版本。

TA貢獻1843條經驗 獲得超7個贊
在我看來,這是一種更容易的方法。
獲取與UI關聯的TaskScheduler。
var UISyncContext = TaskScheduler.FromCurrentSynchronizationContext();
然后在上面的UISyncContext上啟動一個新任務。
Task.Factory.StartNew(() => { /* Do your UI stuff here; */}, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, UISyncContext);
- 3 回答
- 0 關注
- 630 瀏覽
添加回答
舉報