WPF - 集成HandyControl UI组件库
WPF - 集成HandyControl UI组件库
CM+Fody+HC是WPF开发的最强组合
环境:net6 + wpf + HandyControl
一. 创建项目
二. 安装 NuGet 包
dotnet add package HandyControl
三. 引入 HandyControl 资源字典
在你的 WPF 项目中的 App.xaml
或主题文件中
1. 引入 HandyControl 资源字典。
2. 添加命名空间
<Application x:Class="BenchMarkMaster.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BenchMarkMaster" xmlns:hc="https://handyorg.github.io/handycontrol" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/> <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
四. 页面代码
在MainWindow.xaml中添加命名空间
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl"
MainWindow.xaml代码:
<Window x:Class="laser.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:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl" xmlns:local="clr-namespace:laser" mc:Ignorable="d" Title="主页" Height="450" Width="800"> <Grid> <VirtualizingStackPanel> <Button Content="这是一个按钮"/> <Button Content="连接" Width="60" Height="25" Margin="0 20 0 0" Background="#FF6BA731" Foreground="White" BorderBrush="#FFAFAEAE" BorderThickness="1" controls:BorderElement.CornerRadius="0"/> </VirtualizingStackPanel> </Grid> </Window>
五. 集成HandyControl提示框
创建 Views文件夹,创建文件 MessageBoxWindow.xaml
<Window x:Class="BenchMarkMaster.Views.MessageBoxWindow" 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:BenchMarkMaster.Views" xmlns:hc="https://handyorg.github.io/handycontrol" mc:Ignorable="d" Title="提示" Height="450" Width="800"> <Grid> <VirtualizingStackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button Width="100" Click="SuccessAction" Height="30" Margin="0 0 10 0">成功</Button> <Button Width="100" Click ="FailAction" Height="30">失败</Button> </VirtualizingStackPanel> </Grid> </Window>
MessageBoxWindow.xaml.cs
using System; using System.Collections.Generic; 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; using MessageBox = HandyControl.Controls.MessageBox; namespace BenchMarkMaster.Views { /// <summary> /// MessageBoxWindow.xaml 的交互逻辑 /// </summary> public partial class MessageBoxWindow : Window { public MessageBoxWindow() { InitializeComponent(); } private void SuccessAction(object sender, RoutedEventArgs e) { MessageBox.Show("成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } private void FailAction(object sender, RoutedEventArgs e) { MessageBox.Show("失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error); } } }
在MainWindow.xaml 中增加按钮
<Button Content="打开弹窗示例" Name="mbWin" Click="mbWin_Click"/>
MainWindow.xaml.cs
private void mbWin_Click(object sender, RoutedEventArgs e) { var mb = new MessageBoxWindow(); mb.Show(); }
效果
引用:https://mp.weixin.qq.com/s?__biz=MzIxMTUzNzM5Ng==&mid=2247493994&idx=1&sn=afaa764eb31c01fc9260673c869cc5e6&scene=21#wechat_redirect
引用:https://github.com/handyOrg/HandyControl?tab=readme-ov-file
引用:https://blog.csdn.net/forcj/article/details/142253036
引用:https://handyorg.github.io/handycontrol/native_controls/groupBox/