应用程序调用了一个为不同线程编组的接口。 windows 8.1 商店应用

the application called an interface that was marshalled for a different thread. windows 8.1 store app

我正在使用 C# 和 xaml 开发 windows 商店 8.1 应用程序。 当我尝试从代码隐藏文件更新 UI 时,出现异常

" the application called an interface that was marshalled for a different thread"

我添加了以下代码来更新 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 中显示 "Offline sync completed..." 消息。

如何在方法执行后在 UI 中显示它?

有人可以帮我吗?

提前致谢

如果您评论Task.Delay(1000),"Offline sync completed..."会在很短的时间内显示,但会立即消失,因为您设置了statustextblk.Text = ""。

所以可以参考@Raymond在评论中说的。在 () => 之前添加修饰符“async”,在 Task.Delay(1000) 之前添加修饰符“await”; 可以参考我做的demo如下:

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 = "";
        }
        );
    }