1、LoginViewModel.cs
/// <summary>
/// 进度条
/// </summary>
private int progressBarValue;
public int ProgressBarValue
{
get { return progressBarValue; }
set
{
progressBarValue = value;
this.RaisePropertyChanged("ProgressBarValue");
}
}
2、绑定到前端“LoginView.xaml”:
<ProgressBar Value="{Binding ProgressBarValue,Mode=TwoWay}" x:Name="pb" Grid.Row="3" Minimum="0" Background="#eee"></ProgressBar>
3、LoginViewModel.cs的登录方法里面添加“进度条”的逻辑:
/// <summary>
/// 登录-事件绑定
/// </summary>
private BaseCommand loginClick;
public BaseCommand LoginClick
{
get
{
if (loginClick == null)
{
loginClick = new BaseCommand(new Action<object>(o =>
{
Task.Run(() =>
{
//进度条逻辑
while (ProgressBarValue < 100)
{
ProgressBarValue += 1;
Thread.Sleep(10);//Thread开辟线程
}
//登录逻辑
var i = loginBLL.GetLogin(UserName, Password);
if (i > 0)
{
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
//打开PersonViewModel新窗口
Views.PersonView personView = new Views.PersonView();
personView.Show();
//关闭原来的窗口
ToClose = true;
}));
}
});
}));
}
return loginClick;
}
}
执行结果: