AlertControl

在这里插入图片描述

using GrowlMessage.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;

namespace GrowlMessage
{
    public class Growl
    {
        private const double topOffset = 20;
        private const double leftOffset = 355;
        public static readonly MainWindow growlNotifications = new MainWindow();

        /// <summary>
        /// 初始化窗体提示组件
        /// </summary>
        public static void InitGrowl()
        {
            growlNotifications.Top = SystemParameters.WorkArea.Top + topOffset;
            growlNotifications.Left = SystemParameters.WorkArea.Right - leftOffset;
        }
        /// <summary>
        /// 异常提示
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg)
        {
            DispatcherCur(msg, "Image\\Error.png");
        }
        /// <summary>
        /// 详细信息提示
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(string msg)
        {
            DispatcherCur(msg, "Image\\Info.png");
        }
        /// <summary>
        /// 消息提示
        /// </summary>
        public static void Message(string msg)
        {
            DispatcherCur(msg, "Image\\Message.png");
        }
        /// <summary>
        /// 网络问题提示
        /// </summary>
        /// <param name="msg"></param>
        public static void NetWork(string msg)
        {
            DispatcherCur(msg, "Image\\Network-Error.png");
        }
        /// <summary>
        /// 成功提示
        /// </summary>
        /// <param name="msg"></param>
        public static void Success(string msg)
        {
            DispatcherCur(msg, "Image\\Success.png");
        }
        /// <summary>
        /// 警告提示
        /// </summary>
        /// <param name="msg"></param>
        public static void Warring(string msg)
        {
            DispatcherCur(msg, "Image\\Warring.png");
        }
        /// <summary>
        /// 当程序关闭时,调用通知关闭
        /// </summary>
        public static void Colse()
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
            {
                growlNotifications.Close();
            }));
        }

        public static void DispatcherCur(string msg,string image)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
            {
                growlNotifications.AddNotification(new Notification
                {
                    Image = AppDomain.CurrentDomain.BaseDirectory + image,
                    Message = msg,
                    Date = DateTime.Now,
                });
            }));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GrowlMessage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 最大通知数量
        /// </summary>
        private const byte MAX_NOTIFICATIONS = 10;
        private int count;
        public Notifications Notifications = new Notifications();
        private readonly Notifications buffer = new Notifications();
        public MainWindow()
        {
            InitializeComponent();
            NotificationsControl.DataContext = Notifications;
        }
        public void AddNotification(Notification notification)
        {
            try
            {
                notification.ID = count++;
                if (Notifications.Count + 1 > MAX_NOTIFICATIONS)
                    buffer.Add(notification);
                else
                    Notifications.Add(notification);

                //Show window if there're notifications
                if (Notifications.Count > 0 && !IsActive)
                    Show();
            }
            catch (Exception ex)
            {
                //LogHelper.Error(ex.Message, ex);
            }

        }
        public void RemoveNotification(Notification notification)
        {
            try
            {
                if (Notifications.Contains(notification))
                    Notifications.Remove(notification);

                if (buffer.Count > 0)
                {
                    Notifications.Add(buffer[0]);
                    buffer.RemoveAt(0);
                }
                //Close window if there's nothing to show
                if (Notifications.Count < 1)
                    Hide();
            }
            catch (Exception ex)
            {
                //LogHelper.Error(ex.Message, ex);
            }
        }
        private void NotificationWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height != 0.0)
                return;
            var element = sender as Grid;
            RemoveNotification(Notifications.FirstOrDefault(n => n.ID == Int32.Parse(element.Tag.ToString())));
        }
    }
}

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrowlMessage
{
    public class Notification : INotifyPropertyChanged
    {
        private int id;
        public int ID
        {
            get { return id; }
            set
            {
                if (id == value) return;
                id = value;
                OnPropertyChanged(nameof(ID));
            }
        }
        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (message == value) return;
                message = value;
                OnPropertyChanged(nameof(Message));
            }
        }
        private DateTime? date;
        public DateTime? Date
        {
            get { return date; }
            set
            {
                if (date == value) return;
                date = value;
                OnPropertyChanged(nameof(Date));
            }
        }
        private string image;
        public string Image
        {
            get { return image; }

            set
            {
                if (image == value) return;
                image = value;
                OnPropertyChanged(nameof(Image));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class Notifications : ObservableCollection<Notification> { }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值