c#使用wpf实现helloworld和login登录

说明:c#使用wpf实现helloworld和login登录

效果图:

step1:C:\Users\wangrusheng\RiderProjects\WpfApp1\WpfApp1\MainWindow.xaml.cs

csharp 复制代码
using System.Windows;

namespace WpfApp1;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void BtnLogin_Click(object sender, RoutedEventArgs e)
    {
        string username = txtUsername.Text.Trim();
        string password = txtPassword.Password;

        // 输入验证
        if (string.IsNullOrEmpty(username))
        {
            MessageBox.Show("Please enter username", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            txtUsername.Focus();
            return;
        }

        if (string.IsNullOrEmpty(password))
        {
            MessageBox.Show("Please enter password", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            txtPassword.Focus();
            return;
        }

        // 简单验证逻辑(实际项目应使用加密验证)
        if (username == "admin" && password == "123456")
        {
            MessageBox.Show("Login successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            // 这里可以跳转到主界面
        }
        else
        {
            MessageBox.Show("Invalid username or password", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            txtPassword.Password = "";
            txtPassword.Focus();
        }
    }
}

step2:C:\Users\wangrusheng\RiderProjects\WpfApp1\WpfApp1\MainWindow.xaml

xml 复制代码
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="用户登录" Height="450" Width="800"
        WindowStartupLocation="CenterScreen">
 
        
        <Grid>
                <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="2*"/>
                </Grid.ColumnDefinitions>

                <!-- Title -->
                <TextBlock Text="User Login" 
                           Grid.ColumnSpan="2"
                           FontSize="24"
                           FontWeight="Bold"
                           HorizontalAlignment="Center"
                           Margin="0 20"
                           Grid.Row="0"/>

                <!-- Username -->
                <TextBlock Text="Username:" 
                           Grid.Row="1"
                           VerticalAlignment="Center"
                           Margin="10"
                           FontSize="16"/>
                <TextBox x:Name="txtUsername" 
                         Grid.Row="1"
                         Grid.Column="1"
                         Margin="10"
                         Padding="5"
                         FontSize="16"
                         VerticalContentAlignment="Center"/>

                <!-- Password -->
                <TextBlock Text="Password:" 
                           Grid.Row="2"
                           VerticalAlignment="Center"
                           Margin="10"
                           FontSize="16"/>
                <PasswordBox x:Name="txtPassword" 
                             Grid.Row="2"
                             Grid.Column="1"
                             Margin="10"
                             Padding="5"
                             FontSize="16"
                             VerticalContentAlignment="Center"/>

                <!-- Login Button -->
                <Button x:Name="btnLogin" 
                        Content="Login" 
                        Grid.Row="3"
                        Grid.ColumnSpan="2"
                        Margin="10"
                        Padding="10 5"
                        FontSize="16"
                        Click="BtnLogin_Click"
                        IsDefault="True"/>
        </Grid>


</Window>

step3:上面的登录写完了,下面是helloworld

C:\Users\wangrusheng\RiderProjects\WpfApp1\WpfApp1\MainWindow.xaml.cs

csharp 复制代码
using System.Text;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApp1;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

step4:C:\Users\wangrusheng\RiderProjects\WpfApp1\WpfApp1\MainWindow.xaml

xml 复制代码
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Text="Hello World!" 
                   HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   FontSize="24"/>
    </Grid>
</Window>

end

相关推荐
学逆向的1 小时前
C 堆区内存管理
c语言·开发语言·网络安全
huoyueyi1 小时前
《融天AI-APP从0到1开发实录》
开发语言·lua
OpenAnolis小助手3 小时前
PHP 两个严重漏洞曝光,龙蜥社区 24 小时完成 3 版本修复
开发语言·操作系统·php·龙蜥社区
阿尼卡~4 小时前
VS Code 配置 C 语言开发环境(Windows 10/11):从 GCC 安装到运行、断点调试,一篇搞定
c语言·开发语言·windows
hhhjhl5 小时前
Undici 跨端三方库仓颉适配全流程
java·开发语言·ubuntu
染指11105 小时前
158.Agent-LangGraph-子图(共享状态和不共享状态模式)
开发语言·人工智能·python·ui·agent·agents
wangchunyu1145 小时前
JAVA JVM内存结构与运行时数据区
java·开发语言·jvm
slongzhang_5 小时前
linux 递归删除源码文件|递归删除目录下 *.js,但排除 *.min.js
linux·开发语言·javascript
云和数据.ChenGuang7 小时前
Python 常见特殊方法详解:__new__、__init__、__del__、__str__、__repr__ 与 __getattribute__
开发语言·人工智能·python·1024程序员节
xyouyuweizhu7 小时前
字节流与字符流:Java IO 的两条主线该怎么选
java·开发语言·python