Reputation: 934
I am trying to refresh/update the UI of a Metro Style app to reflect changes.
To redraw the Controls in Windows Forms, you can simply use:
this.Refresh();
How can I achieve a similar result in Metro Style apps?
Consider this simple example in C#:
private async void Button_Click(object sender, RoutedEventArgs e)
{
btnStatus.Content = "Test started";
Task.Delay(3000); // Wait 3 seconds
btnStatus.Content = "Test Ended"
}
What I need to do is: show the 'start' message, wait a few seconds and then show the 'end' message. However, when this runs, btnStatus immediately shows 'TestEnded' - as if the first 2 statements didn't execute.
How can I fix this?
I have looked at: How to refresh the UI in a metro app? but it didn't help.
Upvotes: 1
Views: 1903
Reputation: 5633
The code you wrote is incorrect. Task.Delay() runs async, so your code naturally continues on.
This will work correctly...
private async void Button_Click(object sender, RoutedEventArgs e)
{
btnStatus.Content = "Test started";
await Task.Delay(3000); // Wait 3 seconds
btnStatus.Content = "Test Ended"
}
Note the addition of the await key world
Upvotes: 6