做一个超市通信息管理系统,用WPF来做。搭了登录页面
这里登录直接获取数据库的登录,如果没有用户,就新增用户,如果有用户那进行判断和比对账号和密码是否正确,正确登录成功,错误登录失败。
View层代码:
登录按钮和关闭按钮都是重写的。
<!--用户名的下拉框-->
<Border Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="4" BorderBrush="#FF00B4FF" BorderThickness="1.4" CornerRadius="4" Margin="0,5,6,5">
<DockPanel>
<Image Source="Images\Login\Login06.png" Margin="10 10 6 8"/>
<Image Source="Images\Login\Login14.png" Width="2" Margin="5"/>
<ComboBox x:Name="BtnName" IsEditable="True" DisplayMemberPath="LoginName"
Width="238" Height="30" HorizontalAlignment="Center" Margin="0,2,0,1"
Style="{StaticResource LoginComboBox_Style}" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
BorderThickness="0" ItemsSource="{Binding CombboxList}"
SelectedItem="{Binding LoginModel}" Padding="10,7.5,0,0"
Text="{Binding LoginModel.LoginName,UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}"/>
</DockPanel>
</Border>
<!--密码框-->
<Border Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="4" BorderBrush="#FF989CA1" BorderThickness="1" CornerRadius="4" Margin="0,5,6,5">
<DockPanel>
<Image Source="Images\Login\Login13.png" Margin="12 10 5.5 8"/>
<Image Source="Images\Login\Login16.png" Width="2" Margin="5"/>
<PasswordBox x:Name="BtnPassword" BorderThickness="0" Width="238" Height="30"
VerticalAlignment="Center" HorizontalAlignment="Center" MaxLength="16"
VerticalContentAlignment="Center" HorizontalContentAlignment="Left"
BorderBrush="Transparent" passwordbox:PassWordHelper.Attach="True"
Padding="0,0,0,0" passwordbox:PassWordHelper.Password="{Binding
LoginModel.Password,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
</DockPanel>
</Border>
<!--记住密码-->
<CheckBox x:Name="CboRememberPassword" IsChecked="{Binding
LoginModel.RememberPassword,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" VerticalAlignment="Center"
BorderBrush="#00A0E8" Foreground="#898989" Height="20" Content="记 住 密 码"
Style="{StaticResource ChackboxFontStyle}" FontSize="13"/>
<!--登录按钮-->
<local:ButtonEx Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2" Content="登 录"
ButtonType="LoginButton" Foreground="#00a2ff" x:Name="BtnLogin" Width="130"
HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="16"
BorderBrush="#00a2ff" BorderThickness="0.2" Command="{Binding SubmitCmd}"
FontFamily="{StaticResource YaHeiLight}"Click="BtnLogin_Click" Height="40"
Margin="0,1"/>
<!--关闭按钮-->
<local:ButtonEx Grid.Column="3" Grid.Row="5" Grid.ColumnSpan="2" x:Name="BtnExit"
Content="关 闭" ButtonType="LoginButton" Foreground="#00a2ff" FontSize="16"
HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="#00a2ff"
BorderThickness="0.2" Click="BtnExit_Click" Height="40" Width="130"
Margin="0,1,6,1" FontFamily="微软雅黑" Command="{Binding SubmitCmd}"/>
Model层代码:
主要存放登录数据
private string loginName;
public string LoginName
{
get { return loginName; }
set{
loginName = value;
RaisePropertyChanged(() => LoginName);
}
}
///<summary>
/// 密码
/// </summary>
private string password;
public string Password
{
get{return password; }
set{
password = value;
RaisePropertyChanged(() => Password);
}
}
///<summary>
/// 记住密码
/// </summary>
private bool rememberPassword;
public bool RememberPassword
{
get{return rememberPassword; }
set{
rememberPassword = value;
RaisePropertyChanged(() => RememberPassword);
}
}
Viewmodel层代码:
这里只有判断登录代码和新增登录代码,其他的代码,就不一一写出来了,包过变量。
///登录判断
public void Login(string msg)
{try{
if (loginModel.LoginName != null && loginModel.Password != null)
{
string Password = EncryptionDecryption.MD5Encrypt(loginModel.Password,
DataCenterManager.KEY_PASSWORD_STRING);
DataTable User =
DBManager.Default.UserDbo.SelectUserAccount(loginModel.LoginName);
if (User.Rows.Count > 0)
{
if (loginModel.LoginName ==
User.Rows[0]["userAccount"].ToString().Trim() && Password
== User.Rows[0]["userPassword"].ToString().Trim())
{
Messenger.Default.Send<string>(loginModel.LoginName,
MessageCenter.MSG_LOGIN_SUCCESS);
Messenger.Default.Send<string>(loginModel.LoginName,
MessageCenter.MSG_LOGIN_USERACCOUNT);
SaveUsersSettings();
}else{
msg = "账号密码错误";
Messenger.Default.Send<string>(msg,
MessageCenter.MSG_LOGIN_FAILD);
}
}else{
msg = "账号密码不存在";
Messenger.Default.Send<string>(msg,
MessageCenter.MSG_LOGIN_FAILD);
}
}else{
if (loginModel.LoginName == null)
{ msg = "请输入账号";}
else if (loginModel.Password == null)
{msg = "请输入密码";}
Messenger.Default.Send<string>(msg, MessageCenter.MSG_LOGIN_FAILD);
}
}
catch (Exception)
{
msg = "账号或密码为空";
Messenger.Default.Send<string>(msg, MessageCenter.MSG_LOGIN_FAILD);
}
}
/// <summary>
/// 保存登录信息
/// </summary>
private void SaveUsersSettings()
{
string Password = EncryptionDecryption.MD5Encrypt(loginModel.Password,
DataCenterManager.KEY_PASSWORD_STRING);
if (Properties.Settings.Default.users.Contains(loginModel.LoginName))
{
int index = Properties.Settings.Default.users.IndexOf
(loginModel.LoginName);
Properties.Settings.Default.users.RemoveAt(index);
Properties.Settings.Default.pwds.RemoveAt(index);
}
Properties.Settings.Default.users.Insert(0, loginModel.LoginName);
if (loginModel.RememberPassword)
{
Properties.Settings.Default.pwds.Insert(0, Password);
}else{
Properties.Settings.Default.pwds.Insert(0, "");}
Properties.Settings.Default.Save();
}
登录页面