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
包地址: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();}}}
}
界面演示
功能特性對比表格
特性 | Show (局部) | ShowGlobal (全局) | 說明 |
---|---|---|---|
顯示位置 | 指定容器內 | 屏幕右上角 | 局部在應用內,全局在整個系統 |
容器要求 | 需要Panel容器 | 無需容器 | 全局自動創建容器 |
多顯示器 | 只顯示在當前窗口 | 支持多顯示器 | 全局彈窗在主顯示器顯示 |
生命周期 | 隨窗口關閉 | 獨立存在 | 全局彈窗不依賴父窗口 |
使用場景 | 應用內提示 | 全局通知 | 根據需求選擇合適的方式 |
自定義樣式參數表格
參數 | 類型 | 默認值 | 說明 |
---|---|---|---|
duration | int | 3000ms | 自動關閉時間,0表示不自動關閉 |
customBackground | Brush | null | 自定義背景顏色,覆蓋默認樣式 |
customForeground | Brush | null | 自定義文字和圖標顏色 |
message | string | 必需 | 提示消息內容,支持多行文本 |
type | AlertType | 必需 | 消息類型,決定默認樣式和圖標 |
高級用法
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);
注意事項
- 線程安全: 所有彈窗操作都會自動切換到UI線程執行
- 性能優化: 彈窗使用淡入淡出動畫,避免突兀顯示
- 內存管理: 彈窗關閉后會自動清理資源
- Z-index: 全局彈窗始終置頂,不會被其他窗口遮擋
總結
MessageBoxGuo
提供了一個簡單易用但功能強大的彈窗解決方案,支持多種消息類型、自定義樣式和顯示位置。無論是應用內提示還是全局通知,都能滿足各種業務場景的需求。
通過合理的參數配置,您可以輕松創建出符合品牌風格的提示框,提升用戶體驗和應用的專業性。