我正在使用C#和xaml开发一个Windows应用商店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中显示它?
有人可以帮帮我吗?
提前致谢
答案 0 :(得分:3)
如果您评论Task.Delay(1000),"脱机同步已完成......"将在很短的时间内显示,但会立即消失,因为你设置了statustextblk.Text =""。
因此你可以参考@Raymond在评论中说。在()=>之前添加修饰符'async'和修饰符'await'在Task.Delay(1000)之前; 您可以参考我制作的演示如下:
private async void Button_Click(object sender, RoutedEventArgs e)
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
statustextblk.Text = "Offline sync in progress...";
}
);
Debug.WriteLine("Refresh started");
await Task.Delay(1000);
//RefreshSuccess = true;
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async () =>
{
statustextblk.Text = "Offline sync completed...";
await Task.Delay(1000);
statustextblk.Text = "";
}
);
}