找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2932|回复: 12

少量代码设计一个登录界面 - .NET CORE(C#) WPF开发

 火.. [复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1355

回帖

2万

积分

管理员

积分
21304
发表于 2021-3-13 19:33:24 | 显示全部楼层 |阅读模式
少量代码设计一个登录界面 - .NET CORE(C#) WPF开发
阅读导航
  • 本文背景
  • 代码实现
  • 本文参考
  • 源码
1. 本文背景
继续 MaterialDesignThemes 开源控件库学习,本文简单使用输入控件的水印附加属性:
materialDesign:HintAssist.Hint。




2. 代码实现
使用 .NET CORE 3.1 创建名为 “Login” 的WPF模板项目,添加1个Nuget库:
MaterialDesignThemes.3.1.0-ci981。
解决方案主要文件目录组织结构:
  • LoginApp.xamlMainWindow.xamlMainWindow.xaml.cs
2.1 App.xaml文件引入样式
文件【App.xaml】,在 StartupUri 中设置启动的视图【MainWindow.xaml】,并在【Application.Resources】节点增加 MaterialDesignThemes库的样式文件:
<Application x:Class="Login.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:local="clr-namespaceogin"             StartupUri="MainWindow.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.Indigo.xaml" />            </ResourceDictionary.MergedDictionaries>        </ResourceDictionary>    </Application.Resources></Application>2.2 MainWindow.xaml 登录窗体
文件【MainWindow.xaml】,设计登录主界面,代码量很小,源码如下:
<Window x:Class="Login.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        mc:Ignorable="d"        Title="登录" Height="500" Width="350"         ResizeMode="NoResize"         WindowStartupLocation="CenterScreen"         WindowStyle="None"         MouseLeftButtonDown="MoveWindow_MouseLeftButtonDown"        FontFamily="Segoe UI Emoji">    <Grid>        <Rectangle Height="280" VerticalAlignment="Top">            <Rectangle.Fill>                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">                    <GradientStop Color="#FF2281D1"/>                    <GradientStop Color="#FF34268A" Offset="1"/>                    <GradientStop Color="#FF33288B" Offset="0.546"/>                </LinearGradientBrush>            </Rectangle.Fill>        </Rectangle>        <Rectangle Width="280" Height="240" VerticalAlignment="Bottom" Margin="0,80" RadiusY="10" RadiusX="10" Fill="White">            <Rectangle.Effect>                <DropShadowEffect BlurRadius="15" Direction="0" RenderingBias="Quality" ShadowDepth="1" Color="#FFBBBBBB"/>            </Rectangle.Effect>        </Rectangle>        <Grid VerticalAlignment="Bottom" Margin="35,80" Height="240">            <Label Content="登录" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" Foreground="Gray" FontSize="18"/>            <StackPanel VerticalAlignment="Center" Margin="15">                <TextBox Margin="0,10" materialDesign:HintAssist.Hint="账号" Style="{StaticResource MaterialDesignFloatingHintTextBox}" FontFamily="Champagne & Limousines" FontSize="18"/>                <asswordBox Margin="0,10" materialDesign:HintAssist.Hint="密码" Style="{StaticResource MaterialDesignFloatingHintPasswordBox}" FontFamily="Champagne & Limousines" FontSize="18"/>            </StackPanel>        </Grid>        <Button Width="150" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,65" Content="LOGIN"/>        <TextBlock Text="忘记密码?" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="30" Foreground="Gray" Cursor="Hand"/>        <Button HorizontalAlignment="Right" VerticalAlignment="Top" Background="{x:Null}" BorderBrush="{x:Null}" Click="Close_Click">            <materialDesignackIcon Kind="Close"/>        </Button>        <Image Source="https://img.dotnet9.com/logo-foot.png" Width="100" Height="100" VerticalAlignment="Top" Margin="30"/>    </Grid></Window>
下面是后台代码:文件【MainWindow.xaml.cs】,关闭窗体、窗体移动等事件处理。
using System.Windows;using System.Windows.Input;namespace Login{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void MoveWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)        {            DragMove();        }        private void Close_Click(object sender, RoutedEventArgs e)        {            this.Close();        }    }}3.本文参考
  • 视频一:C# WPF Material Design UI: Login Window,配套源码:Login2。
  • C# WPF开源控件库《MaterialDesignInXAML》
4.源码
演示代码已全部奉上,为了方便演示,代码中的图片使用本站外链,代码可直接拷贝并按代码结构组织编译即可运行。

工控课堂 www.gkket.com

0

主题

120

回帖

551

积分

中级会员

积分
551
发表于 2021-3-16 02:41:57 | 显示全部楼层
感恩无私的分享与奉献
工控课堂 www.gkket.com

0

主题

110

回帖

328

积分

注册会员

积分
328
发表于 2021-3-18 02:25:46 | 显示全部楼层
绝对干货,楼主给力,支持了!!!
工控课堂 www.gkket.com

0

主题

79

回帖

120

积分

新手上路

积分
120
发表于 2025-11-14 03:30:53 | 显示全部楼层
已转发给朋友,一起感受这份快乐~
工控课堂 www.gkket.com

0

主题

546

回帖

1736

积分

高级会员

积分
1736
发表于 2025-11-14 11:16:43 | 显示全部楼层
楼主太会说了,字字句句都在理~
工控课堂 www.gkket.com

0

主题

78

回帖

125

积分

新手上路

积分
125
发表于 2025-11-14 12:57:43 | 显示全部楼层
救命!这回复笑到我捶桌,必须置顶~
工控课堂 www.gkket.com

0

主题

162

回帖

386

积分

注册会员

积分
386
发表于 2025-11-14 14:05:03 | 显示全部楼层
不请自来,就想夸一句:太顶了!
工控课堂 www.gkket.com

0

主题

138

回帖

232

积分

注册会员

积分
232
发表于 2025-11-14 14:06:21 | 显示全部楼层
同款经历!简直是世另我
工控课堂 www.gkket.com

0

主题

86

回帖

135

积分

新手上路

积分
135
发表于 2025-11-15 07:40:17 | 显示全部楼层
哈哈哈哈笑不活,楼主这脑洞绝了!
工控课堂 www.gkket.com

0

主题

110

回帖

166

积分

新手上路

积分
166
发表于 2025-11-15 07:50:42 | 显示全部楼层
赞同 + 10086,完全说出了我的想法!
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 21:12 , Processed in 0.099739 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表