【发布时间】:2016-09-28 09:24:07
【问题描述】:
我正在使用 C# 和 xaml 开发一个 Windows Store 8.1 应用程序。 当我尝试从文件后面的代码更新 UI 时,我遇到了一个异常,即
“应用程序调用了一个为不同线程编组的接口”
我添加了以下代码来更新 UI
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
statustextblk.Text = "Offline sync in progress...";
}
);
这工作正常。但是我想在离线同步完成后更新相同的 textblack。所以为此我写了下面的代码,代码看起来像这样
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
statustextblk.Text = "Offline sync in progress...";
}
);
await SharedContext.Context.OfflineStore.ScheduleFlushQueuedRequestsAsync();
Debug.WriteLine("Refresh started");
if (SharedContext.Context.OfflineStore != null && SharedContext.Context.OfflineStore.Open)
await SharedContext.Context.OfflineStore.ScheduleRefreshAsync();
RefreshSuccess = true;
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
statustextblk.Text = "Offline sync completed...";
Task.Delay(1000);
statustextblk.Text = "";
}
);
但它没有在 UI 中显示“离线同步完成...”消息。
方法执行后如何在 UI 中显示?
有人可以帮帮我吗?
提前致谢
【问题讨论】:
-
你没有
awaitTask.Delay(1000)所以下一行(清除消息)立即运行。 -
嘿雷蒙德.. 感谢您的回复。它甚至没有在 UI 中显示“离线同步完成...”这条消息。我评论了 Task.Delay(1000);这条线也是
标签: c# windows-runtime windows-store-apps winrt-xaml