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

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

概述

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

快速开始

nuget

复制代码
dotnet add package MessageBoxGuo --version 1.0.1

Install-Package MessageBoxGuo -Version 1.0.1

包地址:https://www.nuget.org/packages/MessageBoxGuo

git地址:https://gitee.com/gwhsss/auto-cad.-entity-tools

安装与引用

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

csharp 复制代码
using MessageBoxGuo;

基本用法

csharp 复制代码
// 显示一个成功提示(局部)
AlertBox.Show("操作成功!", AlertType.Success, AlertContainer);

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

功能特性详细说明

AlertType 枚举

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

方法参数详解

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

完整示例代码

XAML 布局

xml 复制代码
<Window
    x: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>
        <!-- 按钮区域 -->
        <StackPanel
            Width="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弹框容器 -->
        <Canvas
            x:Name="AlertContainer"
            Width="300"
            Height="0"
            Margin="20"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"/>
    </Grid>
</Window>

C# 代码实现

csharp 复制代码
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();
            }
        }
    }
}

界面演示

功能特性对比表格

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

自定义样式参数表格

参数 类型 默认值 说明
duration int 3000ms 自动关闭时间,0表示不自动关闭
customBackground Brush null 自定义背景颜色,覆盖默认样式
customForeground Brush null 自定义文字和图标颜色
message string 必需 提示消息内容,支持多行文本
type AlertType 必需 消息类型,决定默认样式和图标

高级用法

1. 长时间显示的提示

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

2. 完全自定义样式

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

3. 手动关闭的提示

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

注意事项

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

总结

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

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