C# 按钮不更新
我有一个非常简单的窗口,靠近托盘中的一个红色球(见下图) 当我点击退出按钮(esci)时,我想在序列化时更改两个botton的内容C# 按钮不更新,c#,wpf,button,text,C#,Wpf,Button,Text,我有一个非常简单的窗口,靠近托盘中的一个红色球(见下图) 当我点击退出按钮(esci)时,我想在序列化时更改两个botton的内容 private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { string strButtonName = (sender as Button).Name; switch (strButtonName.ToUpper()) { case "BTEXIT
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
string strButtonName = (sender as Button).Name;
switch (strButtonName.ToUpper())
{
case "BTEXIT":
string strError;
this.btShow.Content = "Serializing";
this.btExit.Content = "Please wait";
this.UpdateLayout();
Serializers.Logger.WriteLog_Reflection<PCDmisLogEntry>;
System.Threading.Thread.Sleep(2000);<---simulates a long serialization process
Environment.Exit(0);
break;
}
this.Close();
}
private void按钮\u单击(对象发送者,System.Windows.RoutedEventArgs e)
{
字符串strButtonName=(发送方作为按钮)。名称;
开关(strButtonName.ToUpper())
{
案例“BTEXIT”:
弦杆;
this.btShow.Content=“序列化”;
this.btExit.Content=“请稍候”;
this.UpdateLayout();
Serializers.Logger.WriteLog_反射;
System.Threading.Thread.Sleep(2000);将代码更改为
case "BTEXIT":
string strError;
this.btShow.Content = "Serializing";
this.btExit.Content = "Please wait";
Dispatcher.Invoke((Action)(() => { }), DispatcherPriority.Render);
this.UpdateLayout();
Serializers.Logger.WriteLog_Reflection<PCDmisLogEntry>;
System.Threading.Thread.Sleep(2000);
Environment.Exit(0);
break;
案例“BTEXIT”:
弦杆;
this.btShow.Content=“序列化”;
this.btExit.Content=“请稍候”;
Invoke((Action)(()=>{}),DispatcherPriority.Render);
this.UpdateLayout();
Serializers.Logger.WriteLog_反射;
系统线程线程睡眠(2000);
环境。退出(0);
打破
我需要一个线程来更新按钮吗?!你不需要线程来更改按钮,但你可以将最后一部分放在线程中以退出应用程序。当你在单击事件处理程序中时,你是在UI线程上,因此在你完成手柄之前不会出现绘画。谢谢,这很好。请你详细说明一下问题出在哪里?这样做只会强制更新更高的优先级吗?因此,在需要强制更新时,这是一种始终可以使用的解决方案吗?