Files
community/Ink Canvas/MainWindow_cs/MW_Notification.cs
T
2026-02-13 13:20:50 +08:00

49 lines
1.6 KiB
C#

using Ink_Canvas.Helpers;
using System;
using System.Linq;
using System.Threading;
using System.Windows;
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
private int lastNotificationShowTime;
private int notificationShowTime = 2500;
public static void ShowNewMessage(string notice, bool isShowImmediately = true)
{
(Application.Current?.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow)
?.ShowNotification(notice, isShowImmediately);
}
public void ShowNotification(string notice, bool isShowImmediately = true)
{
try
{
if (TextBlockNotice == null || GridNotifications == null)
{
return;
}
lastNotificationShowTime = Environment.TickCount;
TextBlockNotice.Text = notice;
AnimationsHelper.ShowWithSlideFromBottomAndFade(GridNotifications);
new Thread(() =>
{
Thread.Sleep(notificationShowTime + 300);
if (Environment.TickCount - lastNotificationShowTime >= notificationShowTime)
Application.Current.Dispatcher.Invoke(() =>
{
AnimationsHelper.HideWithSlideAndFade(GridNotifications);
});
}).Start();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"ShowNotification 异常: {ex.Message}", LogHelper.LogType.Error);
}
}
}
}