简体   繁体   中英

How to refresh the UI of a Metro Style app?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM