WPF 在进行时不更新文本框
我有这样的代码:
void wait(int ms)
{
System.Threading.Thread.Sleep(ms);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
info.Text = "step 1";
wait(1000);
info.Text = "step 2";
wait(1000);
info.Text = "step 3";
wait(1000);
info.Text = "step 4";
wait(1000);
}
问题是 textbox.text 在整个 void button1_Click 完成后更新。它没有在播出中更新:(
请问,该怎么做?
I have this code:
void wait(int ms)
{
System.Threading.Thread.Sleep(ms);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
info.Text = "step 1";
wait(1000);
info.Text = "step 2";
wait(1000);
info.Text = "step 3";
wait(1000);
info.Text = "step 4";
wait(1000);
}
And problem is that textbox.text is updated after whole void button1_Click finished. It is not updated ON AIR :(
Please, How to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就这样做吧。
Just do this.
在
button1_Click
方法返回之前,GUI 线程不会刷新。这就是为什么您只能看到最后一个值。您必须将长方法放入异步调用中或使用线程。The GuI Thread won't refresh until the
button1_Click
method returns. That's why you only see the last value. You have to put long methods into asynchronous calls or use threads.测量/排列(以及渲染)是异步发生的,因此如果您想强制屏幕更新,则需要调用 UpdateLayout。
The measure/arrange (and therefore the render) happens asynchronously so if you want to force the screen to update then you would need to call UpdateLayout.
如果您尝试使用 DispatcherFrame 来模拟 DoEvents 将完成的形式会怎样:
您可能需要包含 System.Security.Permissions 和 System.Windows.Threading。之后,在每次睡眠后调用 DoEvents() 即可获得所需的结果。
链接到 MSDN 文章:http://msdn。 microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx
What if you tried a DispatcherFrame to simulate what forms DoEvents would accomplish:
You may need to include System.Security.Permissions and System.Windows.Threading. After that pplace a call to DoEvents() after each of your sleeps and you get the desired result.
Link to MSDN article: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx