首先在App.Config文件夹中加入以下代码:
<appSettings>
<add key="userName" value="admin"/>
<add key="password" value="1"/>
<add key="isRemember" value="true"/>
</appSettings>
xaml代码:
<TextBox InputMethod.IsInputMethodEnabled="False" Margin="20 10" materialDesign:HintAssist.Hint="账号" x:Name="txtUserNumber" MaxLength="11"/>
<PasswordBox Margin="20 10" materialDesign:HintAssist.Hint="密码" x:Name="txtPassword"/>
<Grid Margin="20 0">
<CheckBox Content="记住我" HorizontalAlignment="Left" x:Name="ckbRemember"/>
<Button x:Name="btnRegister" Content="注册" FontFamily="Aa语文老师的字" Foreground="#fa7dac" Style="{StaticResource MaterialDesignFlatButton}" HorizontalAlignment="Right" Click="BtnRegister_Click"/>
</Grid>
<Button x:Name="btnLogin" Content="登录" Margin="20 30" Background="#cdafff" BorderBrush="#cdafff" Click="BtnLogin_Click"/>
控制器代码:
using Emperor_System.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Emperor_System.View.RegisterLogin
{
/// <summary>
/// Login.xaml 的交互逻辑
/// </summary>
public partial class Login : Window
{
public Login()
{
InitializeComponent();
txtUserNumber.Text = GetSettingString("userNumber");
txtPassword.Password = GetSettingString("password");
if (GetSettingString("isRemember") == "true")
{
ckbRemember.IsChecked = true;
}
else
{
ckbRemember.IsChecked = false;
}
}
EmperorSystem_WPFEntities myModel = new EmperorSystem_WPFEntities();
//private void Button_Click(object sender, RoutedEventArgs e)
//{
// if (Convert.ToBoolean(ckbRemember.IsChecked))
// {
// UpdateSettingString("userNumber", txtUserNumber.Text);
// UpdateSettingString("password", txtPassword.Password);
// UpdateSettingString("isRemember", "true");
// MessageBox.Show("登录并记住密码!");
// }
// else
// {
// UpdateSettingString("userNumber", "");
// UpdateSettingString("password", "");
// UpdateSettingString("isRemember", "");
// MessageBox.Show("登录不记住密码!");
// }
//}
/// <summary>
/// 读取客户设置
/// </summary>
/// <param name="settingName"></param>
/// <returns></returns>
public static string GetSettingString(string settingName)
{
try
{
string settingString = ConfigurationManager.AppSettings[settingName].ToString();
return settingString;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 更新设置
/// </summary>
/// <param name="settingName"></param>
/// <param name="valueName"></param>
public static void UpdateSettingString(string settingName, string valueName)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[settingName] != null)
{
config.AppSettings.Settings.Remove(settingName);
}
config.AppSettings.Settings.Add(settingName, valueName);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
//页面数据清空
private void EmptyControl()
{
txtUserNumber.Text = string.Empty;
txtPassword.Password = string.Empty;
}
//注册按钮(转到注册页面)
private void BtnRegister_Click(object sender, RoutedEventArgs e)
{
Register myRegister = new Register();
myRegister.Show();
this.Close();
}
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
int InttxtUserNumber = txtUserNumber.Text.Length;
String StrtxtUserNumber = txtUserNumber.Text;
String intPassword = txtPassword.Password;
try
{
//查询该账号
var selectBagroundUser = (from dbBagroundUser in myModel.PW_BagroundUser
where dbBagroundUser.UserNumber == StrtxtUserNumber.Trim()
select new
{
dbBagroundUser.BagroundUserID,
dbBagroundUser.UserNumber,
dbBagroundUser.Password
}).Single();
if(intPassword == selectBagroundUser.Password)
{
int BagroundUserId1 = selectBagroundUser.BagroundUserID;
string userNumber1 = txtUserNumber.Text;
//登录成功,跳到主页面
MainInterface myLogin = new MainInterface(BagroundUserId1, userNumber1);
myLogin.BagroundUserId = selectBagroundUser.BagroundUserID;
myLogin.userNumber = txtUserNumber.Text;
myLogin.Show();
if (Convert.ToBoolean(ckbRemember.IsChecked))
{
UpdateSettingString("userNumber", txtUserNumber.Text);
UpdateSettingString("password", txtPassword.Password);
UpdateSettingString("isRemember", "true");
this.Close();
}
else
{
UpdateSettingString("userNumber", "");
UpdateSettingString("password", "");
UpdateSettingString("isRemember", "");
this.Close();
}
}
else
{
MessageBox.Show("您输入的或密码不正确!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception)
{
MessageBox.Show("您输入的帐号不存在!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
//页面控件初始化
EmptyControl();
}
}
}
}