【问题标题】:Refreshing parent after calling ContentDialog in WP 8.1在 WP 8.1 中调用 ContentDialog 后刷新父级
【发布时间】:2014-12-24 14:35:17
【问题描述】:

我正在从 WP 8.1 应用程序中调用 ContentDialog,该应用程序从用户那里收集数据并将其写入应用程序数据。

然后我从 Application Data 中重新读取变量并将它们显示在父级的 GUI 中。

private void Settings_Click(object sender, RoutedEventArgs e)
{
     new AppSettings(); // Open dialog
     dataInit(); // Re-read the data from AppData
     guiInit(); // Populate GUI /w new data
}

由于某种原因,在我用新数据关闭 ContentDialog 后,GUI 没有刷新。如果我打开 ContentDialog 并再次关闭它,无论我是否修改任何数据,它都会刷新。

我觉得 ContentDialog 是异步运行的,但据我所知,打开它会阻止执行,因为它在同一个线程中运行。有什么想法吗?

【问题讨论】:

    标签: c# windows-phone-8.1


    【解决方案1】:

    您的代码不是 async 并且您无需等待用户的选择 - 您会显示对话框并在用户关闭它之前更新 UI。看看这个:

    private async void Settings_Click(object sender, RoutedEventArgs e)  // async void hence it's an event
    {
         await new MessageDialog("Wait for user").ShowAsync(); // Open dialog
         dataInit(); // Re-read the data from AppData
         guiInit(); // Populate GUI /w new data
    }
    

    在上面的代码中,进一步的执行是等到 MessageDialog 关闭。我不确定你的new AppSettings(); 中有什么,但如果你想等待用户的选择,那么你需要异步实现它或以某种不同的方式实现它。

    【讨论】:

    • 首先,感谢您的帮助。现在可以了。但为什么我需要任务?我将其更改为 private void async 并将 await 添加到调用对话框的行中,它可以正常工作。当我将此函数转换为 Task 时,我收到有关错误返回类型的错误。
    • @Alex 是的,当您有按钮事件并且它必须是异步无效时,这是极少数情况 - 我在上面的代码中错了。然而,当你编写一个方法时,你应该总是返回一个 Task,你不能 await void 并且在异常处理方面存在差异 - more informationmore here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多