首页 > 其他分享 >WPF修改MessageBox样式(.NET6版本)

WPF修改MessageBox样式(.NET6版本)

时间:2024-06-16 13:54:14浏览次数:12  
标签:MessageBox HandyControl 样式 using NET6 WPF ViewAViewModel 全局

blog-hbh-hc-header

一、问题场景

使用HandyControl简写HC 作为基础UI组件库时,希望系统中所有的MessageBox 样式都使用HCMessageBox,常规操作如下:
在对应的xxxx.cs 顶部使用using 指定特定类的命名空间。

using MessageBox = HandyControl.Controls.MessageBox;

这样,当前页面对应的MessageBox 就都是对应指定类型的控件实例。这样做虽然能够解决样式问题,弊端也比较明显,如果项目页面较多,交互复杂度高一些,需要修改的页面工作量也就大了。粘贴复制,粘贴复制也是需要时间和成本的。以前的一种解决方式是通过获取原生MessageBox句柄(钩子)方式对样式进行替换实现全局的处理。

二、解决方案

NET6时,引入了全局GlobalUsings,用于实现程序集项目中对目标库的全局声明和引用。
需要对项目文件进行启用<ImplicitUsings>enable</ImplicitUsings>,配置如下:

<Project Sdk="Microsoft.NET.Sdk">
<!--省略配置内容-->
  <PropertyGroup>
   <!--省略配置内容-->
    <UseWPF>true</UseWPF>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <!--引入UI库Nuget依赖-->
	<PackageReference Include="HandyControl" Version="3.5.1" />
</ItemGroup>

<!--省略配置内容-->
</Project>

默认情况下启用之后,项目的全局引用会自动在本地生成编译文件,为方便管理,可以创建一个GlobalUsings.cs文件用于全局引用的可控管理,类文件名字实际并无特定要求,只是参考其他项目,这个名字看起来好识别。
而类文件中就是包含的着全局的引用配置,关键字使用global using

global using MessageBox = HandyControl.Controls.MessageBox;

这样编译生成,当前项目能够尽可能少的去添加头部引用。
案例代码ViewAViewModel.cs

public class ViewAViewModel : BindableBase
{

    public DelegateCommand UrlCommand { get; private set; }
    public ViewAViewModel()
    {
        UrlCommand = new DelegateCommand(GetUrl);
    }

    private void GetUrl()
    {
       MessageBox.Show("测试成功","消息提示");
       throw new NotImplementedException();
    }
}

程序效果如下:
image.png
弹框效果:
image.png

标签:MessageBox,HandyControl,样式,using,NET6,WPF,ViewAViewModel,全局
From: https://www.cnblogs.com/guanguanchangyu/p/18250562

相关文章

  • WPF Path GeometryCombineMode Union, Exclude,Intersect,Xor
    union<Windowx:Class="WpfApp172.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF StreamGeometry
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usin......
  • WPF Path Data PathGeometry PathFigure Segments BezierSegment,LineSegment,ArcSeg
     BezierSegment//BezierCurveusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documen......
  • WPF Stretch None,Fill,Uniform,UnformToFill
    None, Thecontentpreservesitsoriginalsize.<ImageSource="/WpfApp169;component/cl.jpg"Stretch="None"/> Fill,Thecontentisresizedtofillthedestinationdimensions.Theaspectratioisnotpreserved.<ImageSource=......
  • WPF Image Image clip EllipseGeometry
    <Windowx:Class="WpfApp169.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF CanFreeze,Freeze(),IsFrozen frozen clone,freeze() it is in a read-only state
    <Windowx:Class="WpfApp168.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Path LineGeometry,RectangleGeometry,EllipseGeometry
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows......
  • WPF RenderTargetBitmap DrawingVisual DrawingContext DrawImage DrawRectangle Draw
    usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;......
  • WPF Image ZoomIn ZoomOut Pan/Move Rotate
    <Windowx:Class="WpfApp162.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • wpfui:一个开源免费具有现代化设计趋势的WPF控件库
    wpfui介绍wpfui是一款开源免费(MIT协议)具有现代化设计趋势的WPF界面库。wpfui为wpf的界面开发提供了流畅的体验,提供了一个简单的方法,让使用WPF编写的应用程序跟上现代设计趋势。截止写这篇文章,该项目获得了6.7kstarts。最近我也在使用wpfui,整体使用下来感觉非常不错,因此想写一......