You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.9 KiB
C#
137 lines
3.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
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 SocketExample
|
|
{
|
|
/// <summary>
|
|
/// LoginWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class LoginWindow : Window
|
|
{
|
|
// 定义事件
|
|
public event Action LoginSuccess;
|
|
public event Action LoginFailed;
|
|
|
|
// 最大尝试次数
|
|
private const int MAX_ATTEMPTS = 2;
|
|
|
|
// 当前尝试次数
|
|
private int _attemptCount = 0;
|
|
|
|
// 正确的用户名和密码(实际应用中应从数据库或配置中读取)
|
|
private const string CORRECT_USERNAME = "haiweiadmin";
|
|
private const string CORRECT_PASSWORD = "haiweinb123";
|
|
|
|
public LoginWindow()
|
|
{
|
|
InitializeComponent();
|
|
UpdateAttemptsDisplay();
|
|
|
|
// 设置焦点到用户名输入框
|
|
txtUsername.Focus();
|
|
|
|
// 绑定回车键事件
|
|
txtPassword.KeyDown += OnPasswordKeyDown;
|
|
}
|
|
|
|
private void OnPasswordKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter)
|
|
{
|
|
AttemptLogin();
|
|
}
|
|
}
|
|
|
|
private void btnLogin_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
AttemptLogin();
|
|
}
|
|
|
|
private void btnCancel_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
// 取消登录,关闭程序
|
|
LoginFailed?.Invoke();
|
|
this.Close();
|
|
}
|
|
|
|
private void AttemptLogin()
|
|
{
|
|
string username = txtUsername.Text.Trim();
|
|
string password = txtPassword.Password;
|
|
|
|
// 验证输入
|
|
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
|
{
|
|
ShowError("用户名和密码不能为空!");
|
|
return;
|
|
}
|
|
|
|
// 验证用户名和密码
|
|
if (username == CORRECT_USERNAME && password == CORRECT_PASSWORD)
|
|
{
|
|
// 登录成功
|
|
LoginSuccess?.Invoke();
|
|
this.Close();
|
|
}
|
|
else
|
|
{
|
|
// 登录失败
|
|
_attemptCount++;
|
|
UpdateAttemptsDisplay();
|
|
|
|
if (_attemptCount >= MAX_ATTEMPTS)
|
|
{
|
|
// 超过最大尝试次数
|
|
MessageBox.Show($"登录失败超过{MAX_ATTEMPTS}次,程序即将关闭!",
|
|
"登录失败",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Error);
|
|
|
|
LoginFailed?.Invoke();
|
|
this.Close();
|
|
}
|
|
else
|
|
{
|
|
ShowError($"用户名或密码错误!剩余尝试次数:{MAX_ATTEMPTS - _attemptCount}");
|
|
txtPassword.Password = "";
|
|
txtPassword.Focus();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ShowError(string message)
|
|
{
|
|
lblError.Text = message;
|
|
lblError.Visibility = Visibility.Visible;
|
|
}
|
|
|
|
private void UpdateAttemptsDisplay()
|
|
{
|
|
int remainingAttempts = MAX_ATTEMPTS - _attemptCount;
|
|
lblAttempts.Text = remainingAttempts > 0
|
|
? $"剩余尝试次数:{remainingAttempts}"
|
|
: "已超过最大尝试次数";
|
|
}
|
|
|
|
protected override void OnClosed(EventArgs e)
|
|
{
|
|
base.OnClosed(e);
|
|
|
|
// 清理事件绑定
|
|
txtPassword.KeyDown -= OnPasswordKeyDown;
|
|
}
|
|
}
|
|
}
|