Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Controls/Toast/ToastControl.xaml.cs
ShrlAlgo 955a01f564 整理
2025-08-20 12:10:35 +08:00

117 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace NeoUI.Controls;
// 定义通知类型
/// <summary>
/// 该枚举定义了ToastControl控件中可以使用的不同类型的通知。每种类型对应不同的视觉样式和图标用于在用户界面上显示不同性质的消息如成功、信息、警告或错误。
/// </summary>
public enum ToastType
{
/// <summary>
/// 表示成功通知的枚举值。当设置为该值时ToastControl控件将使用预定义的颜色和图标来显示一条成功消息。背景颜色为浅绿色#F0F9EB前景颜色为深绿色#67C23A并且会显示一个表示成功的图标。
/// </summary>
Success,
/// <summary>
/// 表示信息通知的枚举值。当设置为该值时ToastControl控件将使用预定义的颜色和图标来显示一条普通消息。背景颜色为浅蓝色#E6F7FF前景颜色为深蓝色#409EFF并且会显示一个表示信息的图标。
/// </summary>
Info,
/// <summary>
/// 表示错误通知的枚举值。当设置为该值时ToastControl控件将使用预定义的颜色和图标来显示一条错误消息。背景颜色为浅红色#FEF2F2前景颜色为深红色#F56C6C并且会显示一个表示错误的图标。
/// </summary>
Error,
/// <summary>
/// 表示警告通知的枚举值。当设置为该值时ToastControl控件将使用预定义的颜色和图标来显示一条警告消息。背景颜色为浅黄色#FDF6EC前景颜色为橙色#E6A23C并且会显示一个表示警告的图标。
/// </summary>
Warning
}
/// <summary>
/// ToastControl 是一个自定义的 WPF 控件,用于在用户界面中显示简短的通知消息。它支持不同类型的通知(如成功、信息、警告和错误),并能够根据通知类型自动调整其视觉样式。
/// </summary>
public partial class ToastControl
{
/// <summary>
///
/// </summary>
public ToastControl()
{
InitializeComponent();
}
// 依赖属性,用于从外部设置消息和类型
/// <summary>
/// 该依赖属性用于存储和获取ToastControl控件中显示的消息文本。通过设置此属性可以动态地更改用户界面上显示的通知消息内容。
/// </summary>
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
nameof(Message), typeof(string), typeof(ToastControl), new PropertyMetadata(string.Empty, OnMessageChanged));
/// <summary>
/// 该依赖属性用于存储和获取ToastControl控件中显示的通知类型。通过设置此属性可以指定通知的种类如成功、信息、警告或错误从而影响通知的视觉表现比如颜色和图标。
/// </summary>
public static readonly DependencyProperty TypeProperty = DependencyProperty.Register(
nameof(Type), typeof(ToastType), typeof(ToastControl), new PropertyMetadata(ToastType.Info, OnTypeChanged));
/// <summary>
/// 用于获取或设置ToastControl控件中显示的消息文本。通过更改此属性可以动态更新用户界面上的通知消息内容。
/// </summary>
public string Message
{
get => (string)GetValue(MessageProperty);
set => SetValue(MessageProperty, value);
}
/// <summary>
/// 该依赖属性用于存储和获取ToastControl控件中显示的通知类型。通过设置此属性可以指定通知的种类如成功、信息、错误或警告从而影响用户界面上通知的视觉呈现方式。
/// </summary>
public ToastType Type
{
get => (ToastType)GetValue(TypeProperty);
set => SetValue(TypeProperty, value);
}
private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ToastControl)d).MessageTextBlock.Text = (string)e.NewValue;
}
// 当类型改变时,更新颜色和图标
private static void OnTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = (ToastControl)d;
var icon = "";
Brush background = Brushes.Gray;
Brush foreground = Brushes.White;
switch ((ToastType)e.NewValue)
{
case ToastType.Success:
icon = "\uE930"; // Success icon
background = new SolidColorBrush(Color.FromRgb(240, 249, 235));
foreground = new SolidColorBrush(Color.FromRgb(103, 194, 58));
break;
case ToastType.Info:
icon = "\uE946"; // Info icon
background = new SolidColorBrush(Color.FromRgb(237, 246, 253));
foreground = new SolidColorBrush(Color.FromRgb(64, 158, 255));
break;
case ToastType.Warning:
icon = "\uE7BA"; // Warning icon
background = new SolidColorBrush(Color.FromRgb(253, 246, 236));
foreground = new SolidColorBrush(Color.FromRgb(230, 162, 60));
break;
case ToastType.Error:
icon = "\uEA39"; // Error icon
background = new SolidColorBrush(Color.FromRgb(254, 242, 242));
foreground = new SolidColorBrush(Color.FromRgb(245, 108, 108));
break;
}
control.IconTextBlock.Text = icon;
control.IconTextBlock.Foreground = foreground;
control.RootBorder.Background = background;
control.RootBorder.BorderBrush = background;
}
}