1

I'm calling a ContentDialog from a WP 8.1 app that collects data from user and writes it into Application Data.

I then re-read the variables from Application Data and display them in parent's 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
}

For some reason GUI doesn't refresh after I close the ContentDialog with new data. It does refresh if I open ContentDialog and close it a second time regardless of whether I modify any data or not.

I feel like ContentDialog runs asynchronously, but as far as I know opening it blocks execution since it runs in the same thread. Any ideas?

Justin XL
  • 38,763
  • 7
  • 88
  • 133
Alex
  • 23
  • 5

1 Answers1

0

Your code is not async and you don't wait for User's choice - you show dialog and before User closes it, the UI is updated. Take a look at this:

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
}

In the code above, further execution is waiting until the MessageDialog is closed. I'm not sure what you have in your new AppSettings();, but if you want to wait for User's choice, then you need to implement it asynchronously or somehow different.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • First of all, thanks for your help. It works now. But why do I need Task? I changed it to private void async and added await to the line calling the dialog and it works correctly. When I converted this function to Task, I was getting errors about wrong return type. – Alex Dec 24 '14 at 01:06
  • @Alex Yeah, that's the rare case when you have button event and it must be async void - my fault in the code above. Nevertheless when you write a method you should always return a Task, you cannot await void and there are differences in exception handling - [more information](http://stackoverflow.com/a/12144426/2681948) and [more here](http://stackoverflow.com/q/8043296/2681948). – Romasz Dec 24 '14 at 06:34