C# 标签内容不会立即刷新
我已经在MVVM模式中创建了一个启动屏幕。我从app.xaml.cs onstartup事件中显示它,然后显示登录屏幕和隐藏启动屏幕。在登录屏幕上,当用户单击登录按钮时,我更改了启动视图模型的消息,显示启动屏幕和隐藏登录屏幕。之后,如果一切正常,我调用服务验证用户登录,然后再次更改消息更新的splash视图模型,显示splash并关闭登录表单。我的问题是,当我第二次更改启动屏幕消息时,它不会显示在启动屏幕上。下面是我的代码C# 标签内容不会立即刷新,c#,wpf,mvvm,C#,Wpf,Mvvm,我已经在MVVM模式中创建了一个启动屏幕。我从app.xaml.cs onstartup事件中显示它,然后显示登录屏幕和隐藏启动屏幕。在登录屏幕上,当用户单击登录按钮时,我更改了启动视图模型的消息,显示启动屏幕和隐藏登录屏幕。之后,如果一切正常,我调用服务验证用户登录,然后再次更改消息更新的splash视图模型,显示splash并关闭登录表单。我的问题是,当我第二次更改启动屏幕消息时,它不会显示在启动屏幕上。下面是我的代码 public class SplashViewModel:ViewMo
public class SplashViewModel:ViewModelBase
{
private string _strMessage = "Loading";
public string Message { get { return _strMessage; }
set { _strMessage = value; OnPropertyChanged("Message"); } }
}
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.Dispatcher.UnhandledException += (s, d) =>
{
throw new NotImplementedException();
};
ShowSplash();
ShowLogin();
//BootStrapper bs = new BootStrapper();
//bs.Run();
}
private void ShowSplash()
{
sp = new Splash.Splash();
spVM = new Splash.SplashViewModel();
sp.DataContext = spVM;
sp.Show();
}
private void ShowLogin()
{
LoginControl.LoginViewModel lgVM = new LoginControl.LoginViewModel();
lg = new Login();
lg.DataContext = lgVM;
lg.LoginNotification += (s) =>
{
if (s == Infrastructure.Enums.Status.Success)
{
//sp.Message = "Loading Modules....";
//sp.ShowMessage();
lg.Close();
//sp.Show();
BootStrapper bs = new BootStrapper();
bs.Run();
}
else if(s==Infrastructure.Enums.Status.Canceled)
{
lg.Close();
sp.Close();
}
};
lg.SplashController += (show, message) =>
{
if (show)
{
spVM.Message = message;
//sp.Message = message;
//sp.ShowMessage();
sp.Show();
//sp.UpdateLayout();
}
else
sp.Hide();
};
sp.Hide();
lg.Show();
}
Login.xaml.cs
public Action<Infrastructure.Enums.Status> LoginNotification { get; set; }
public Action<bool, string> SplashController { get; set; }
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
try
{
SplashController.Invoke(true, "Authenticating...");
txtLoging.GetBindingExpression(TextBox.TextProperty).UpdateSource();
txtPassword.GetBindingExpression(Infrastructure.AttachedProperty.PasswordAssistent.PasswordValue).UpdateSource();
bool? a = txtLoging.GetValue(Validation.HasErrorProperty) as bool?;
bool? b = txtPassword.GetValue(Validation.HasErrorProperty) as bool?;
if (!a.Value && !b.Value)
{
Hide();
Service.DBOperationServiceContractClient service = new Service.DBOperationServiceContractClient();
var parm = new Service.DataContract.DBParametersDataContract()
{
ProcedureName = "AuthenticateUser",
Params = new List<Service.Entity.DBParameter>() { new Service.Entity.DBParameter() {ParameterName= "@username", ParameterValue=txtLoging.Text,ParamerterType= DbType.String }
,new Service.Entity.DBParameter() {ParameterName= "@passowrd", ParameterValue=txtPassword.Password,ParamerterType= DbType.String }}
};
var output = service.DBReadOperation(parm);
System.Threading.Thread.Sleep(7000);
if (output.ReadValue.Tables.Count > 0 && output.ReadValue.Tables[0].Rows.Count > 0)
{
//Todo Keep logged in user details
SplashController.Invoke(true, "Loading Modules....");
//System.Threading.Thread.Sleep(7000);
LoginNotification.Invoke(Infrastructure.Enums.Status.Success);
}
}
}
catch (FaultException<Service.FaultContract.Fault> ex)
{
throw ex;
}
catch (Exception exc)
{
throw exc;
}
}
公共操作登录说明{get;set;}
公共操作控制器{get;set;}
私有void btnLogin\u单击(对象发送者,路由目标e)
{
尝试
{
SplashController.Invoke(true,“身份验证…”);
GetBindingExpression(TextBox.TextProperty).UpdateSource();
GetBindingExpression(Infrastructure.AttachedProperty.PasswordAssistent.PasswordValue).UpdateSource();
bool?a=txtLoging.GetValue(Validation.HasErrorProperty)作为bool?;
bool?b=txtPassword.GetValue(Validation.HasErrorProperty)作为bool?;
如果(!a.Value&&!b.Value)
{
隐藏();
Service.dbOperationsServiceContractClient服务=新服务.dbOperationsServiceContractClient();
var parm=new Service.DataContract.DBParametersDataContract()
{
ProcedureName=“AuthenticateUser”,
Params=new List(){new Service.Entity.DBParameter(){ParameterName=“@username”,ParameterValue=txtLoging.Text,parametertype=DbType.String}
,new Service.Entity.DBParameter(){ParameterName=“@passowrd”,ParameterValue=txtPassword.Password,parametertype=DbType.String}
};
var输出=service.DBReadOperation(parm);
系统线程线程睡眠(7000);
if(output.ReadValue.Tables.Count>0&&output.ReadValue.Tables[0].Rows.Count>0)
{
//Todo保持登录用户详细信息
SplashController.Invoke(true,“加载模块…”);
//系统线程线程睡眠(7000);
LoginNotification.Invoke(Infrastructure.Enums.Status.Success);
}
}
}
捕获(FaultException-ex)
{
掷骰子;
}
捕获(异常exc)
{
抛出exc;
}
}
我知道我已经在代码隐藏中编写了用于登录的代码,该代码将根据MVVM规则将代码移动到视图模型中
请务必让我知道如何立即在启动屏幕上显示消息。
throw exc代码>不要这样做。您正在从一个格式完全正确的异常中删除stacktrace。使用justthrow代码>如果你想重新抛出一个异常(尽管你应该先问问自己为什么要捕获它们)。你是否在任何地方开始一个线程或任务?只要代码在UI线程中运行,UI就不会更新。只有在完成并返回控制后,才会处理消息并更新UI。非常感谢您的帮助throw exc代码>不要这样做。您正在从一个格式完全正确的异常中删除stacktrace。使用justthrow代码>如果你想重新抛出一个异常(尽管你应该先问问自己为什么要捕获它们)。你是否在任何地方开始一个线程或任务?只要代码在UI线程中运行,UI就不会更新。只有在完成并返回控制后,才会处理消息并更新UI。非常感谢您的帮助