Files

64 lines
2.5 KiB
C#
Raw Permalink Normal View History

2026-02-13 13:20:50 +08:00
using Ink_Canvas.Helpers;
2025-08-31 11:43:52 +08:00
using System;
2025-05-25 09:29:48 +08:00
using System.Linq;
using System.Threading;
using System.Windows;
2025-08-03 16:46:33 +08:00
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
2025-07-28 14:40:44 +08:00
private int lastNotificationShowTime;
2025-05-25 09:29:48 +08:00
private int notificationShowTime = 2500;
/// <summary>
/// 静态方法,用于在主窗口中显示通知
/// </summary>
/// <param name="notice">要显示的通知文本</param>
/// <param name="isShowImmediately">指示是否应立即显示通知</param>
/// <remarks>
/// 该方法会:
/// 1. 获取应用程序中的主窗口实例
/// 2. 调用主窗口的ShowNotification方法显示通知
/// </remarks>
2025-08-03 16:46:33 +08:00
public static void ShowNewMessage(string notice, bool isShowImmediately = true)
{
2025-05-25 09:29:48 +08:00
(Application.Current?.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow)
?.ShowNotification(notice, isShowImmediately);
}
/// <summary>
/// 在窗口中显示带从底部滑入并淡入的通知文本,并在配置的时长后自动隐藏(若未被新通知覆盖)。
/// </summary>
/// <param name="notice">要显示的通知文本。</param>
/// <param name="isShowImmediately">指示是否应立即显示通知;当前实现默认立即显示。</param>
2025-08-03 16:46:33 +08:00
public void ShowNotification(string notice, bool isShowImmediately = true)
{
try
{
2026-02-13 13:20:50 +08:00
if (TextBlockNotice == null || GridNotifications == null)
{
return;
}
2025-05-25 09:29:48 +08:00
lastNotificationShowTime = Environment.TickCount;
TextBlockNotice.Text = notice;
AnimationsHelper.ShowWithSlideFromBottomAndFade(GridNotifications);
2025-08-03 16:46:33 +08:00
new Thread(() =>
{
2025-05-25 09:29:48 +08:00
Thread.Sleep(notificationShowTime + 300);
if (Environment.TickCount - lastNotificationShowTime >= notificationShowTime)
2025-08-03 16:46:33 +08:00
Application.Current.Dispatcher.Invoke(() =>
{
2025-05-25 09:29:48 +08:00
AnimationsHelper.HideWithSlideAndFade(GridNotifications);
});
2025-07-28 14:40:44 +08:00
}).Start();
2025-05-25 09:29:48 +08:00
}
2026-02-13 13:20:50 +08:00
catch (Exception ex)
{
LogHelper.WriteLogToFile($"ShowNotification 异常: {ex.Message}", LogHelper.LogType.Error);
}
2025-05-25 09:29:48 +08:00
}
}
}