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

WPF Alert弹框控件 - 完全使用指南

WPF Alert弹框控件 - 完全使用指南

    • 概述
    • 快速开始
      • nuget
      • 安装与引用
      • 基本用法
    • 功能特性详细说明
      • AlertType 枚举
      • 方法参数详解
        • Show 方法(局部弹窗)
        • ShowGlobal 方法(全局弹窗)
    • 完整示例代码
      • XAML 布局
      • C# 代码实现
    • 界面演示
    • 功能特性对比表格
    • 自定义样式参数表格
    • 高级用法
      • 1. 长时间显示的提示
      • 2. 完全自定义样式
      • 3. 手动关闭的提示
    • 注意事项
    • 总结

概述

MessageBoxGuo 是一个功能强大的WPF弹框控件库,提供了现代化的Alert提示功能,支持局部和全局两种显示模式,以及多种消息类型和自定义选项。

快速开始

nuget

dotnet add package MessageBoxGuo --version 1.0.1
Install-Package MessageBoxGuo -Version 1.0.1

MessageBoxGuo

包地址:https://www.nuget.org/packages/MessageBoxGuo
git地址:https://gitee.com/gwhsss/auto-cad.-entity-tools

安装与引用

首先在项目中引用 MessageBoxGuo 命名空间:

using MessageBoxGuo;

基本用法

// 显示一个成功提示(局部)
AlertBox.Show("操作成功!", AlertType.Success, AlertContainer);// 显示一个错误提示(全局)
AlertBox.ShowGlobal("操作失败,请重试。", AlertType.Error);

功能特性详细说明

AlertType 枚举

类型说明默认颜色图标
Success成功提示#67C23A (绿色)
Error错误提示#F56C6C (红色)
Warning警告提示#E6A23C (橙色)
Info信息提示#909399 (灰色)

方法参数详解

Show 方法(局部弹窗)
AlertBox.Show(string message,           // 必需:要显示的消息内容AlertType type,           // 必需:消息类型Panel container,          // 必需:承载弹窗的容器(如Canvas)int duration = 3000,      // 可选:自动关闭时间(毫秒),默认3000Brush customBackground = null,  // 可选:自定义背景色Brush customForeground = null   // 可选:自定义前景色(文字和图标颜色)
);
ShowGlobal 方法(全局弹窗)
AlertBox.ShowGlobal(string message,           // 必需:要显示的消息内容AlertType type,           // 必需:消息类型int duration = 3000,      // 可选:自动关闭时间(毫秒),默认3000Brush customBackground = null,  // 可选:自定义背景色Brush customForeground = null   // 可选:自定义前景色(文字和图标颜色)
);

完整示例代码

XAML 布局

<Windowx:Class="MessageBoxGuoTests.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:local="clr-namespace:MessageBoxGuoTests"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="WPF Alert弹框示例"Width="800"Height="600"mc:Ignorable="d"><Grid><!-- 按钮区域 --><StackPanelWidth="300"HorizontalAlignment="Center"VerticalAlignment="Center"><!-- 局部弹窗测试按钮 --><Button Content="成功提示(局部)" Click="SuccessButton_Click" Style="{StaticResource SuccessButtonStyle}"/><Button Content="错误提示(局部)" Click="ErrorButton_Click" Style="{StaticResource ErrorButtonStyle}"/><Button Content="警告提示(局部)" Click="WarningButton_Click" Style="{StaticResource WarningButtonStyle}"/><Button Content="信息提示(局部)" Click="InfoButton_Click" Style="{StaticResource InfoButtonStyle}"/><Button Content="自定义提示(局部)" Click="CustomButton_Click" Style="{StaticResource CustomButtonStyle}"/><Separator Height="20" Margin="10"/><!-- 全局弹窗测试按钮 --><Button Content="成功提示(全局)" Click="GlobalSuccessButton_Click" Style="{StaticResource SuccessButtonStyle}"/><Button Content="错误提示(全局)" Click="GlobalErrorButton_Click" Style="{StaticResource ErrorButtonStyle}"/><Button Content="警告提示(全局)" Click="GlobalWarningButton_Click" Style="{StaticResource WarningButtonStyle}"/><Button Content="信息提示(全局)" Click="GlobalInfoButton_Click" Style="{StaticResource InfoButtonStyle}"/><Button Content="自定义提示(全局)" Click="GlobalCustomButton_Click" Style="{StaticResource CustomButtonStyle}"/><Separator Height="20" Margin="10"/><!-- 批量测试按钮 --><Button Content="测试多个弹窗" Click="TestMultipleAlerts_Click" Style="{StaticResource TestButtonStyle}"/><Button Content="测试多个全局弹窗" Click="TestMultipleGlobalAlerts_Click" Style="{StaticResource TestButtonStyle}"/></StackPanel><!-- Alert弹框容器 --><Canvasx:Name="AlertContainer"Width="300"Height="0"Margin="20"HorizontalAlignment="Right"VerticalAlignment="Top"/></Grid>
</Window>

C# 代码实现

using MessageBoxGuo;
using System.Windows;
using System.Windows.Threading;namespace MessageBoxGuoTests
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}// 局部弹窗示例private void SuccessButton_Click(object sender, RoutedEventArgs e){AlertBox.Show("操作成功!", AlertType.Success, AlertContainer);}private void ErrorButton_Click(object sender, RoutedEventArgs e){AlertBox.Show("操作失败,请重试。", AlertType.Error, AlertContainer);}private void WarningButton_Click(object sender, RoutedEventArgs e){AlertBox.Show("警告:请注意操作权限。", AlertType.Warning, AlertContainer);}private void InfoButton_Click(object sender, RoutedEventArgs e){AlertBox.Show("提示:系统将于今晚进行维护。", AlertType.Info, AlertContainer);}private void CustomButton_Click(object sender, RoutedEventArgs e){AlertBox.Show("自定义消息:这是一个自定义样式的提示框。", AlertType.Info, AlertContainer, 5000, Brushes.Purple, Brushes.White);}// 全局弹窗示例private void GlobalSuccessButton_Click(object sender, RoutedEventArgs e){AlertBox.ShowGlobal("全局操作成功!", AlertType.Success);}private void GlobalErrorButton_Click(object sender, RoutedEventArgs e){AlertBox.ShowGlobal("全局操作失败,请重试。", AlertType.Error);}private void GlobalWarningButton_Click(object sender, RoutedEventArgs e){AlertBox.ShowGlobal("全局警告:请注意操作权限。", AlertType.Warning);}private void GlobalInfoButton_Click(object sender, RoutedEventArgs e){AlertBox.ShowGlobal("全局提示:系统将于今晚进行维护。", AlertType.Info);}private void GlobalCustomButton_Click(object sender, RoutedEventArgs e){AlertBox.ShowGlobal("全局自定义消息:这是一个自定义样式的提示框。", AlertType.Info, 5000, Brushes.Purple, Brushes.White);}// 批量测试private void TestMultipleAlerts_Click(object sender, RoutedEventArgs e){for (int i = 1; i <= 5; i++){int index = i;DispatcherTimer timer = new DispatcherTimer{Interval = TimeSpan.FromMilliseconds(index * 500)};timer.Tick += (s, args) =>{timer.Stop();AlertBox.Show($"测试消息 {index}", AlertType.Info, AlertContainer);};timer.Start();}}private void TestMultipleGlobalAlerts_Click(object sender, RoutedEventArgs e){for (int i = 1; i <= 5; i++){int index = i;DispatcherTimer timer = new DispatcherTimer{Interval = TimeSpan.FromMilliseconds(index * 500)};timer.Tick += (s, args) =>{timer.Stop();AlertBox.ShowGlobal($"全局测试消息 {index}", AlertType.Info);};timer.Start();}}}
}

界面演示

MessageBoxGuo

功能特性对比表格

特性Show (局部)ShowGlobal (全局)说明
显示位置指定容器内屏幕右上角局部在应用内,全局在整个系统
容器要求需要Panel容器无需容器全局自动创建容器
多显示器只显示在当前窗口支持多显示器全局弹窗在主显示器显示
生命周期随窗口关闭独立存在全局弹窗不依赖父窗口
使用场景应用内提示全局通知根据需求选择合适的方式

自定义样式参数表格

参数类型默认值说明
durationint3000ms自动关闭时间,0表示不自动关闭
customBackgroundBrushnull自定义背景颜色,覆盖默认样式
customForegroundBrushnull自定义文字和图标颜色
messagestring必需提示消息内容,支持多行文本
typeAlertType必需消息类型,决定默认样式和图标

高级用法

1. 长时间显示的提示

// 显示10秒的提示
AlertBox.Show("重要提示,请仔细阅读。", AlertType.Warning, AlertContainer, 10000);

2. 完全自定义样式

// 使用自定义颜色
AlertBox.Show("自定义样式提示", AlertType.Info, AlertContainer, 4000, Brushes.LightBlue, Brushes.DarkBlue);

3. 手动关闭的提示

// 设置为0表示不自动关闭,需要用户手动关闭
AlertBox.Show("请手动关闭此提示", AlertType.Info, AlertContainer, 0);

注意事项

  1. 线程安全: 所有弹窗操作都会自动切换到UI线程执行
  2. 性能优化: 弹窗使用淡入淡出动画,避免突兀显示
  3. 内存管理: 弹窗关闭后会自动清理资源
  4. Z-index: 全局弹窗始终置顶,不会被其他窗口遮挡

总结

MessageBoxGuo 提供了一个简单易用但功能强大的弹窗解决方案,支持多种消息类型、自定义样式和显示位置。无论是应用内提示还是全局通知,都能满足各种业务场景的需求。

通过合理的参数配置,您可以轻松创建出符合品牌风格的提示框,提升用户体验和应用的专业性。

http://www.lryc.cn/news/626710.html

相关文章:

  • 【力扣 买卖股票的最佳时机 Java/Python】
  • 【Unity3D优化】平衡 Hide 与 Destroy:基于性能等级与 LRU 的 UI 管理策略与实践思考
  • 大数据毕业设计选题推荐-基于Hadoop的电信客服数据处理与分析系统-Spark-HDFS-Pandas
  • 计算机网络模型
  • 华为数通认证学习
  • CSS 定位的核心属性:position
  • SPSS数据文件的建立与管理
  • JAVA中向量数据库(Milvus)怎么配合大模型使用
  • 案例分享:BRAV-7123助力家用型人形机器人,智能生活未来已来
  • vscode连接docker
  • Linux 文本处理与 Shell 编程笔记:正则表达式、sed、awk 与变量脚本
  • React-native之组件
  • 51单片机-驱动LED点阵模块教程
  • Gitee仓库 日常操作详细步骤
  • 【笔记】动手学Ollama 第五章 Ollama 在 LangChain 中的使用 - Python 集成
  • 康师傅2025上半年销售收入减少超11亿元,但净利润增长20.5%
  • Linux《进程间通信(下)》
  • LidaReferv1论文细节解读
  • Linux面试经典题目(七)
  • 在SQL中使用大模型时间预测模型TimesFM
  • 不会写 SQL 也能出报表?积木报表 + AI 30 秒自动生成报表和图表
  • sqlalchemy 是怎么进行sql表结构管理的,怎么进行数据处理的
  • 深度学习核心技巧
  • SQL-leetcode— 2356. 每位教师所教授的科目种类的数量
  • Kafka如何保证「消息不丢失」,「顺序传输」,「不重复消费」,以及为什么会发送重平衡(reblanace)
  • Mybatis执行SQL流程(五)之MapperProxy与MapperMethod
  • 在完全没有无线网络(Wi-Fi)和移动网络(蜂窝数据)的环境下,使用安卓平板,通过USB数据线(而不是Wi-Fi)来控制电脑(版本2)
  • 力扣 hot100 Day79
  • 大数据常见问题分析与解决方案
  • ODPS 十五周年实录 | 为 AI 而生的数据平台