我是Windows Phone开发的新手,我正在尝试做一些我认为非常简单的事情:我有一个页面,一个按钮和一个textBlock。我想,只要按下按钮,textBlock的文本就会变为“Bazinga!”。几秒钟,然后恢复到之前的值。
我已经尝试了下面的代码,但它不起作用(我想因为仍然在Button_Click调用中,textBlock的显示不会刷新)。
在查看几个关键字后,我看到了这一点:WPF not updating textbox while in progress
这告诉我必须显式调用Dispatcher的Invoke方法...但我只看到一个BeginInvoke()方法(我猜这是Windows Phone的一个特性),而且我尝试正确的几次尝试都是不吉利的。 / p>
感谢您的帮助
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;
}
答案 0 :(得分:0)
private async void Button_Click(object sender, RoutedEventArgs e)
{
string text = TextBlock1.Text;
TextBlock1.Text = "Bazinga!";
await Task.Delay(5000);
TextBlock1.Text = text;
}