GPT4辅助小白自学WPF[小项目1_网络卡密验证系统GUI]

以下是当前代码架构的Markdown文档:

# CDKManager 项目代码架构

## 目录结构

```plaintext
CDKManager
├── Models
│   ├── KeyModel.cs
│   ├── UserModel.cs
├── Services
│   ├── DataService.cs
├── Views
│   ├── LoginWindow.xaml
│   ├── LoginWindow.xaml.cs
│   ├── MainWindow.xaml
│   ├── MainWindow.xaml.cs
│   ├── RegisterWindow.xaml
│   ├── RegisterWindow.xaml.cs
│   ├── CreateProductControl.xaml
│   ├── CreateProductControl.xaml.cs
│   ├── GenerateKeyControl.xaml
│   ├── GenerateKeyControl.xaml.cs
│   ├── ManageKeyControl.xaml
│   ├── ManageKeyControl.xaml.cs
│   ├── ViewLogControl.xaml
│   ├── ViewLogControl.xaml.cs
├── Resources
│   ├── Styles.xaml
├── App.xaml
├── App.xaml.cs

文件说明

Models

KeyModel.cs
namespace CDKManager.Models
{
    public class KeyModel
    {
        public string Key { get; set; }
        public string Status { get; set; }
        public DateTime ExpiryDate { get; set; }
        public bool IsSelected { get; set; }
    }
}
UserModel.cs
namespace CDKManager.Models
{
    public class UserModel
    {
        public string Username { get; set; }
        public string Role { get; set; }
        public bool IsActive { get; set; }
        public string Password { get; set; }
    }
}

Services

DataService.cs
using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace CDKManager.Services
{
    public static class DataService
    {
        private static ObservableCollection<KeyModel> keys = new ObservableCollection<KeyModel>();
        private static ObservableCollection<UserModel> users = new ObservableCollection<UserModel>();

        static DataService()
        {
            InitializeDefaultUsers();
        }

        public static ObservableCollection<KeyModel> GetKeys()
        {
            if (keys.Count == 0)
            {
                keys.Add(new KeyModel { Key = "Key1", Status = "Active", ExpiryDate = DateTime.Now.AddDays(10) });
                keys.Add(new KeyModel { Key = "Key2", Status = "Banned", ExpiryDate = DateTime.Now.AddDays(20) });
            }
            return keys;
        }

        public static ObservableCollection<UserModel> GetUsers()
        {
            return users;
        }

        public static bool UserExists(string username)
        {
            return users.Any(user => user.Username == username);
        }

        public static void RegisterUser(string username, string password)
        {
            users.Add(new UserModel { Username = username, Role = "User", IsActive = true, Password = password });
        }

        public static void RemoveUser(UserModel user)
        {
            if (user != null)
            {
                users.Remove(user);
            }
        }

        private static void InitializeDefaultUsers()
        {
            if (!UserExists("admin"))
            {
                users.Add(new UserModel { Username = "admin", Role = "Admin", IsActive = true, Password = "password" });
            }
        }
    }
}

Views

LoginWindow.xaml
<Window x:Class="CDKManager.Views.LoginWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        Title="Login" Height="350" Width="400" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
            <Image Source="logo.png" Height="100" Margin="0,0,0,20"/>
            <TextBox x:Name="UsernameTextBox" materialDesign:HintAssist.Hint="请输入用户名" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <PasswordBox x:Name="PasswordTextBox" materialDesign:HintAssist.Hint="请输入密码" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <Button Content="登录" Height="30" Background="#007BFF" Foreground="White" Margin="0,0,0,10" BorderBrush="Transparent" Click="LoginButton_Click"/>
            <CheckBox Content="记住我" Margin="0,0,0,10"/>
            <TextBlock Text="忘记密码" Foreground="#007BFF" HorizontalAlignment="Right" Cursor="Hand" TextDecorations="Underline"/>
            <Button Content="注册" Height="30" Background="#28A745" Foreground="White" Margin="10,0,0,0" BorderBrush="Transparent" Click="RegisterButton_Click"/>
        </StackPanel>
    </Grid>
</Window>
LoginWindow.xaml.cs
using System.Windows;
using CDKManager.Services;

namespace CDKManager.Views
{
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Password;

            if (ValidateLogin(username, password))
            {
                MainWindow mainWindow = new MainWindow();
                mainWindow.Show();
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }

        private bool ValidateLogin(string username, string password)
        {
            var user = DataService.GetUsers().FirstOrDefault(u => u.Username == username && u.Password == password);
            return user != null;
        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            RegisterWindow registerWindow = new RegisterWindow();
            registerWindow.ShowDialog();
        }
    }
}
RegisterWindow.xaml
<Window x:Class="CDKManager.Views.RegisterWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        Title="Register" Height="400" Width="400" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
            <TextBox x:Name="UsernameTextBox" materialDesign:HintAssist.Hint="请输入用户名" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <PasswordBox x:Name="PasswordTextBox" materialDesign:HintAssist.Hint="请输入密码" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <PasswordBox x:Name="ConfirmPasswordTextBox" materialDesign:HintAssist.Hint="确认密码" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <Button Content="注册" Height="30" Background="#28A745" Foreground="White" Margin="0,0,0,10" BorderBrush="Transparent" Click="RegisterButton_Click"/>
        </StackPanel>
    </Grid>
</Window>
RegisterWindow.xaml.cs
using System.Windows;
using CDKManager.Services;

namespace CDKManager.Views
{
    public partial class RegisterWindow : Window
    {
        public RegisterWindow()
        {
            InitializeComponent();
        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            string username = UsernameTextBox.Text;
            string password = PasswordTextBox.Password;
            string confirmPassword = ConfirmPasswordTextBox.Password;

            if (ValidateRegistration(username, password, confirmPassword))
            {
                if (DataService.UserExists(username))
                {
                    MessageBox.Show("用户名已存在");
                }
                else
                {
                    DataService.RegisterUser(username, password);
                    MessageBox.Show("注册成功");
                    this.Close();
                }
            }
        }

        private bool ValidateRegistration(string username, string password, string confirmPassword)
        {
            if (string.IsNullOrEmpty(username))
            {
                MessageBox.Show("用户名不能为空");
                return false;
            }

            if (password.Length < 8 || password.Length > 16)
            {
                MessageBox.Show("密码长度必须在8到16位之间");
                return false;
            }

            if (password != confirmPassword)
            {
                MessageBox.Show("两次输入的密码不一致");
                return false;
            }

            return true;
        }
    }
}
MainWindow.xaml
<Window x:Class="CDKManager.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CardKeyManager" Height="600" Width="1000" WindowStartupLocation="CenterScreen">
    <Grid>
        <DockPanel>
            <StackPanel DockPanel.Dock="Left" Width="200" Background="#333333">
                <Button Content="创建产品" Height="50" Background="Transparent" Foreground="White" BorderBrush="Transparent" Click="NavigateToCreateProduct"/>
                <Button Content="生成卡密" Height="50" Background="Transparent" Foreground="White" BorderBrush="Transparent" Click="NavigateToGenerateKey"/>
                <Button Content="管理卡密" Height="50" Background="Transparent" Foreground="White" BorderBrush="Transparent" Click="NavigateToManageKey"/>
                <Button Content="查看日志" Height="50" Background="Transparent" Foreground="White" BorderBrush="Transparent" Click="NavigateToViewLog"/>
            </StackPanel>
            <Grid>
                <ContentControl x:Name="MainContent"/>
            </Grid>
        </DockPanel>
    </Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;

namespace CDKManager.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void NavigateToCreateProduct(object sender, RoutedEventArgs e)
        {
            MainContent.Content = new CreateProductControl();
        }

        private void NavigateToGenerateKey(object sender, RoutedEventArgs e)
        {
            MainContent.Content = new GenerateKeyControl();
        }

        private void NavigateToManageKey(object sender, RoutedEventArgs e)
        {
            MainContent.Content = new ManageKeyControl();
        }

        private void NavigateToViewLog(object sender, RoutedEventArgs e)
        {
            MainContent.Content = new ViewLogControl();
        }
    }
}

UserControls

CreateProductControl.xaml
<UserControl x:Class="CDKManager.Views.CreateProductControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             Height="Auto" Width="Auto">
    <Grid Margin="20">
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
            <TextBox x:Name="ProductNameTextBox" materialDesign:HintAssist.Hint="请输入产品名称" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <TextBox x:Name="ProductDescriptionTextBox" materialDesign:HintAssist.Hint="请输入产品描述" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <Button Content="创建" Height="30" Background="#28A745" Foreground="White" BorderBrush="Transparent" Click="CreateProductButton_Click"/>
        </StackPanel>
    </Grid>
</UserControl>
CreateProductControl.xaml.cs
using System.Windows.Controls;

namespace CDKManager.Views
{
    public partial class CreateProductControl : UserControl
    {
        public CreateProductControl()
        {
            InitializeComponent();
        }

        private void CreateProductButton_Click(object sender, RoutedEventArgs e)
        {
            string productName = ProductNameTextBox.Text;
            string productDescription = ProductDescriptionTextBox.Text;

            // 创建产品逻辑
            MessageBox.Show($"产品 '{productName}' 创建成功!");
        }
    }
}
GenerateKeyControl.xaml
<UserControl x:Class="CDKManager.Views.GenerateKeyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             Height="Auto" Width="Auto">
    <Grid Margin="20">
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="300">
            <ComboBox x:Name="ProductComboBox" materialDesign:HintAssist.Hint="请选择产品" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <TextBox x:Name="KeyQuantityTextBox" materialDesign:HintAssist.Hint="请输入生成数量" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <Button Content="生成" Height="30" Background="#007BFF" Foreground="White" BorderBrush="Transparent" Click="GenerateKeyButton_Click"/>
        </StackPanel>
    </Grid>
</UserControl>
GenerateKeyControl.xaml.cs
using System.Windows.Controls;

namespace CDKManager.Views
{
    public partial class GenerateKeyControl : UserControl
    {
        public GenerateKeyControl()
        {
            InitializeComponent();
        }

        private void GenerateKeyButton_Click(object sender, RoutedEventArgs e)
        {
            string selectedProduct = (ProductComboBox.SelectedItem as ComboBoxItem)?.Content.ToString();
            if (int.TryParse(KeyQuantityTextBox.Text, out int quantity))
            {
                // 生成卡密逻辑
                MessageBox.Show($"{quantity} 个卡密生成成功!");
            }
            else
            {
                MessageBox.Show("请输入有效的数量!");
            }
        }
    }
}
ManageKeyControl.xaml
<UserControl x:Class="CDKManager.Views.ManageKeyControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             Height="Auto" Width="Auto">
    <Grid Margin="20">
        <StackPanel>
            <TextBox x:Name="SearchTextBox" materialDesign:HintAssist.Hint="搜索卡密" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <ComboBox x:Name="StatusFilterComboBox" materialDesign:HintAssist.Hint="状态筛选" Margin="0,0,0,10" Height="30" BorderBrush="#D1D1D1" Background="#F2F2F2"/>
            <DataGrid x:Name="KeyDataGrid" AutoGenerateColumns="False" Margin="0,0,0,10">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="卡密" Binding="{Binding Key}" Width="*"/>
                    <DataGridTextColumn Header="状态" Binding="{Binding Status}" Width="*"/>
                    <DataGridTextColumn Header="过期时间" Binding="{Binding ExpiryDate}" Width="*"/>
                </DataGrid.Columns>
            </DataGrid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Button Content="激活" Height="30" Background="#28A745" Foreground="White" BorderBrush="Transparent" Margin="5"/>
                <Button Content="禁用" Height="30" Background="#DC3545" Foreground="White" BorderBrush="Transparent" Margin="5"/>
                <Button Content="删除" Height="30" Background="#FFC107" Foreground="White" BorderBrush="Transparent" Margin="5"/>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>
ManageKeyControl.xaml.cs
using System.Windows.Controls;

namespace CDKManager.Views
{
    public partial class ManageKeyControl : UserControl
    {
        public ManageKeyControl()
        {
            InitializeComponent();
        }

        // 添加对应的激活、禁用和删除逻辑
    }
}
ViewLogControl.xaml
<UserControl x:Class="CDKManager.Views.ViewLogControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="Auto" Width="Auto">
    <Grid Margin="20">
        <StackPanel>
            <DataGrid x:Name="LogDataGrid" AutoGenerateColumns="False" Margin="0,0,0,10">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="*"/>
                    <DataGridTextColumn Header="操作" Binding="{Binding Action}" Width="*"/>
                    <DataGridTextColumn Header="用户" Binding="{Binding User}" Width="*"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="导出日志" Height="30" Background="#007BFF" Foreground="White" BorderBrush="Transparent" HorizontalAlignment="Center"/>
        </StackPanel>
    </Grid>
</UserControl>
ViewLogControl.xaml.cs
using System.Windows.Controls;

namespace CDKManager.Views
{
    public partial class ViewLogControl : UserControl
    {
        public ViewLogControl()
        {
            InitializeComponent();
        }
        // 添加导出日志逻辑
    }
}

Resources

Styles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Button Style -->
    <Style TargetType="Button">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Padding" Value="10,5"/>
        <Setter Property="FontSize" Value="14"/>
        <Setter Property="Background" Value="#FF6200EE"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>

    <!-- TextBox Style -->
    <Style TargetType="TextBox">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>

    <!-- ComboBox Style -->
    <Style TargetType="ComboBox">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>

</ResourceDictionary>

App.xaml

<Application x:Class="CDKManager.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Views.LoginWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
                <ResourceDictionary Source="/Resources/Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml.cs

using System.Windows;

namespace CDKManager
{
    public partial class App : Application
    {
    }
}

说明

用户界面

用户界面由多个窗口和用户控件组成,主要包括:

  • LoginWindow: 用户登录和注册窗口。
  • MainWindow: 主界面,包含导航菜单和内容区域。
  • RegisterWindow: 注册窗口。
  • CreateProductControl: 创建产品的用户控件。
  • GenerateKeyControl: 生成卡密的用户控件。
  • ManageKeyControl: 管理卡密的用户控件。
  • ViewLogControl: 查看日志的用户控件。

数据服务

DataService 类用于管理应用程序中的数据,包括用户和卡密数据。提供了用户注册、验证和数据初始化等功能。

资源

Styles.xaml 文件包含应用程序的样式定义,例如按钮、文本框和组合框的样式。

数据模型

数据模型包括两个主要类:

  • KeyModel: 表示卡密信息,包括卡密字符串、状态、过期日期和选择状态。
  • UserModel: 表示用户信息,包括用户名、角色、激活状态和密码。

样式和主题

应用程序使用了 MaterialDesignInXamlToolkit 提供的 Material Design 样式和主题,以实现现代化的用户界面。资源字典在 App.xaml 中合并,确保在应用程序中全局使用这些样式和主题。

登录逻辑

LoginWindow.xaml.cs 中实现了登录验证逻辑,通过调用 DataService 验证用户名和密码。如果验证通过,将打开主窗口 MainWindow

注册逻辑

RegisterWindow.xaml.cs 中实现了注册逻辑,检查用户名是否存在,并确保密码长度在 8 到 16 位之间。如果用户名不存在且验证通过,将调用 DataService 注册新用户。

主窗口导航

MainWindow.xaml.cs 中实现了导航逻辑,点击不同的按钮可以在主内容区域 MainContent 中加载不同的用户控件,实现功能模块之间的切换。

用户控件

用户控件如 CreateProductControlGenerateKeyControlManageKeyControlViewLogControl 实现了各自的功能逻辑,例如创建产品、生成卡密、管理卡密和查看日志。

依赖项

项目依赖于以下 NuGet 包:

  • MaterialDesignThemes: 提供 Material Design 样式和主题。
  • MaterialDesignColors: 提供 Material Design 颜色样式。

运行和构建

  1. 打开 Visual Studio。
  2. 加载解决方案 CDKManager.sln
  3. 还原 NuGet 包。
  4. 编译解决方案。
  5. 运行项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值