当前位置: 首页 > article >正文

NET WPF使用组件库HandyControl

一、背景

WPF原生控件提供的API功能不够强大,设置一般的功能都需要进行很复杂的配置和实现。

1.1 原生按钮控件

例如,原生控件<Button/> 默认效果是这样的:

MainWindow.xaml代码:

<Window x:Class="wpf_demo.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:wpf_demo"
        mc:Ignorable="d"
        Title="主页" Height="450" Width="800">
    <Grid>
        <VirtualizingStackPanel>
            <Button Content="这是一个按钮"/>
        </VirtualizingStackPanel>
    </Grid>
</Window>

按钮

按钮-鼠标悬浮

  • 问题1:按钮如果不设置高和宽,宽度默认占满窗口。期望有一个默认的常规的高和宽。

  • 问题2:当鼠标悬浮在按钮上时,会默认高亮且颜色为天蓝色。没有提供相关属性来去掉鼠标悬浮效果,且高亮颜色也不能更改。

1.2 自定义按钮控件

为了方便我们自己控制按钮,我们放弃采用原生<Button/> 控件,转而采用<Border/>控件来模拟按钮的效果,但这种实现太过臃肿和复杂。

<Window x:Class="wpf_demo.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:wpf_demo"
        mc:Ignorable="d"
        Title="主页" Height="450" Width="800">
    <Grid>
        <VirtualizingStackPanel>
            <Border Height="25" Width="100" HorizontalAlignment="Left" BorderBrush="#FFAFAEAE" BorderThickness="1" Background="#FF6BA731"  >
                <TextBlock MouseLeftButtonUp="ButtonAction" Text="自定义按钮" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"/>
                <Border.Style>
                    <Style TargetType="Border">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Cursor" Value="Hand" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            </Border>
        </VirtualizingStackPanel>
    </Grid>
</Window>

自定义按钮效果

二、解决方案

使用开源框架覆盖原生控件默认的样式,并丰富控件API

三、使用开源框架HandyControl

以下操作步骤,来源于官网文档 HandyControl官网

3.1 Nuget的方式引用控件库

Nuget

搜索HandyControl,并进行安装

安装HandyControl

安装成功后,在项目的 【依赖项】-【包】下会显示刚才安装的HandyControl,并有对应的版本号。

安装成功

3.2 在App.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>
3.3 在App.xaml中添加命名空间

xmlns:hc="https://handyorg.github.io/handycontrol"

App.xaml完整代码如下:

<Application x:Class="wpf_demo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:hc="https://handyorg.github.io/handycontrol"
             xmlns:local="clr-namespace:wpf_demo"
             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>
3.4 页面代码

在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>
3.5 效果

原来写的原生控件<Button/>代码完全不用动,默认样式就已经被覆盖修改了。效果如下:

效果

四、HandyControl提示框
4.1 MessageBoxWindow.xaml

<Window x:Class="wpf_demo.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:hc="https://handyorg.github.io/handycontrol"
        xmlns:local="clr-namespace:wpf_demo"
        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>
4.2 MessageBoxWindow.xaml.cs


using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace wpf_demo
{
    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);
        }
    }
}

注意,要引入using MessageBox = HandyControl.Controls.MessageBox;,才会覆盖WPF原生提示框效果。

4.3 效果

成功

失败

五、国际化问题
5.1 问题描述

在英文系统下,MessageBox弹框按钮依然显示为中文,如下图【确定】按钮显示未中文。

using MessageBox = HandyControl.Controls.MessageBox;

MessageBox.Show("Success", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);

弹框国际化按钮问题

5.2 解决方案

安装英文版HandyControl

HandyControl英文版

中英文

按钮显示英文


http://www.kler.cn/news/312053.html

相关文章:

  • Github 2024-09-14 Rust开源项目日报Top10
  • 傅里叶变换
  • vim 操作一列数字
  • 【天怡AI-注册安全分析报告-无验证方式导致安全隐患】
  • javascript 浏览器打印不同页面设置方向,横向纵向打印
  • CPLEX+Yalmip+MATLAB2022a配置
  • 【贪心算法】贪心算法一
  • vue前端调起电脑应用并且检测有没有调用成功
  • 人工智能将来好就业吗?
  • LINUX的PHY抽象层——PAL
  • Qt/C++ 了解NTFS文件系统,解析盘符引导扇区数据获取MFT(Master File Table)主文件表偏移地址
  • 服务发现和代理实例的自动更新
  • Linux 基本使用和 web 程序部署 ( 8000 字 Linux 入门 )
  • 【python】后台程序初始化全流程
  • electron-vue安装与打包问题解决
  • js中箭头函数与普通函数的区别
  • 删除视频最后几帧 剪切视频
  • Vue3:el-table实现日期的格式化
  • 安卓 uniapp跨端开发
  • JVM 内存模型:堆、栈、方法区讲解
  • 如何使用Postman搞定带有token认证的接口实战!
  • VSCode C++ Tasks.json中的变量
  • 住宅HTTP代理:提升网络隐私与安全的新选择
  • Electron-vue asar 局部打包优化处理方案——绕开每次npm run build 超级慢的打包问题
  • 1.MySQL在Centos 7环境安装
  • STM32 -中断
  • mysql使用sql函数对json数组的处理
  • 首席数据官(CCRC-CDO)的职业价值
  • 学习最佳实践G4F中的编程技术:获得python项目的当前安装版本
  • 2024年【汽车驾驶员(高级)】考试报名及汽车驾驶员(高级)模拟考试题