简体   繁体   中英

Print transient text in a textBlock: WP8 and dispatcher magic

I am new to Windows Phone development and I am trying to do something which I believe is quite simple: I have a page, with a button and a textBlock. I would like that, whenever the button is pressed, the textBlock's text change to "Bazinga!" for a few seconds and then revert to its previous value.

I have tried the code below but it does not work (I suppose because the textBlock's display is not refreshed while still in the Button_Click call).

After looking up a few keywords, I saw this: WPF not updating textbox while in progress

This tells me I must explicitly call the Dispatcher's Invoke method... but I only see a BeginInvoke() method (I guess this is a specificity of Windows Phone) and my few attempts at getting it right have been unlucky.

Thanks for any help you can

private void Button_Click(object sender, RoutedEventArgs e)
{
    this.Function();
}

private void Function()
{
    string text = this.TextBlock1.Text;
    DateTime until = DateTime.Now.AddSeconds(5.0);
    this.TextBlock1.Text = "Bazinga!";
    while (DateTime.Now < until)
    {
        // Do nothing
    }
    this.TextBlock1.Text = text;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
    string text = TextBlock1.Text;
    TextBlock1.Text = "Bazinga!";
    await Task.Delay(5000);
    TextBlock1.Text = text;
}

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