[WPF] 簡單製作客製化的Notification

2017-09-13

再次,拜見 WPF 。

最近真的因為專案需求要來到WPF ,有一個需求,就是要在Windows 右下方彈出客製化的視窗,結果像是這樣..
samplewpfnoti

原本想一下覺得很難,後來使用網路上OpenSource的套件,其實蠻簡單的,在這邊感謝Open Source的大大,這邊簡單說一下步驟..

1. 透過  Nuget 下載套件 Hardcodet.Wpf.TaskbarNotification  URL: https://www.nuget.org/packages/Hardcodet.Wpf.TaskbarNotification/

圖片 316

2. 製作一個 UserControl
image

3. 回到主畫面,在工具箱加入該DLL 並且拉至主UI
image

XAML Code :

<tb:TaskbarIcon x:Name="tb" HorizontalAlignment="Left" Height="10" Margin="0,0,0,0" VerticalAlignment="Top" Width="10" IconSource="t16.ico"/>

4. 呼叫 UserControl 彈出 :
            var  notiControl = new NotificationItem1();
            
            tb.ShowCustomBalloon(notiControl, PopupAnimation.Slide, 5000);

5. UserControl 關閉Notification 的Code :


using Hardcodet.Wpf.TaskbarNotification;
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 WPFNotificationSample
{
    /// <summary>
    /// NotificationItem1.xaml 的互動邏輯
    /// </summary>
    public partial class NotificationItem1 : UserControl
    {
        public NotificationItem1()
        {
            InitializeComponent();
            TaskbarIcon.AddBalloonClosingHandler(btnClose, btnClose_Click);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true; 
            TaskbarIcon taskbarIcon = TaskbarIcon.GetParentTaskbarIcon(this);
            taskbarIcon.CloseBalloon();
        }
    }
}


比我想像的簡單太多了,如果有需要的可以參考一下..


source code : https://github.com/donma/WPFNotificationSample


當麻許的超技八 2014 | Donma Hsu Design.