【问题标题】:the application called an interface that was marshalled for a different thread. windows 8.1 store app应用程序调用了为不同线程编组的接口。 Windows 8.1 商店应用程序
【发布时间】: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 中显示?

有人可以帮帮我吗?

提前致谢

【问题讨论】:

  • 你没有await Task.Delay(1000) 所以下一行(清除消息)立即运行。
  • 嘿雷蒙德.. 感谢您的回复。它甚至没有在 UI 中显示“离线同步完成...”这条消息。我评论了 Task.Delay(1000);这条线也是

标签: c# windows-runtime windows-store-apps winrt-xaml


【解决方案1】:

如果您评论Task.Delay(1000),“离线同步完成...”会在很短的时间内显示,但会立即消失,因为您设置了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 = "";
        }
        );
    }

【讨论】:

  • 您好,如果解决了您的问题,请标记为回答,方便其他访问者参考,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多