728x90
728x170

Notifications.Wpf 누겟 패키지를 이용하면 간단하게 알림 처리를 할 수 있습니다.

윈도우 알림과 프로그램 알림을 할 수 있어 유용해 보여 소개합니다.

 

1. WPF 프로젝트를 만들고 'Notifications.Wpf' Nuget 패키지를 설치합니다.

 

2. 윈도우에 알림 메시지를 표시하는 경우 아래처럼 코딩합니다.

MainWindow.xaml

<Window
    x:Class="Wpf.NotificationsTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:Wpf.NotificationsTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Button
            Width="150"
            Height="50"
            Click="Button_Click"
            Content="Show Notification!" />
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;

using Notifications.Wpf;

namespace Wpf.NotificationsTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var notificationManager = new NotificationManager();

            notificationManager.Show(new NotificationContent
            {
                Title = "Warning!!",
                Message = "윈도우에 바이러스가 감지되었습니다!",
                Type = NotificationType.Warning
            });
        }
    }
}

결과

 

3. 프로그램에 알림 메세지를 표시하는 경우 아래처럼 코딩합니다.

xaml 단에서 먼저 알림 메세지가 표시될 영역을 지정하고 위치(Position)와 최대 개수(MaxItems)를 지정합니다.

MainWindow.xaml

<Window
    x:Class="Wpf.NotificationsTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:Wpf.NotificationsTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:notifications="clr-namespace:Notifications.Wpf.Controls;assembly=Notifications.Wpf"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Button
            Width="150"
            Height="50"
            Click="Button_Click"
            Content="Show Notification!" />
        <notifications:NotificationArea
            x:Name="WindowArea"
            MaxItems="3"
            Position="BottomRight" />
    </Grid>
</Window>

 

위 선언한 영역을 지정하여 알림을 보내면 해당영역에 표시됩니다.

MainWindow.xaml.cs

using System.Windows;

using Notifications.Wpf;

namespace Wpf.NotificationsTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var notificationManager = new NotificationManager();

            notificationManager.Show(new NotificationContent
            {
                Title = "Information",
                Message = "프로그램에서 윈도우 알림을 보냅니다.",
                Type = NotificationType.Information
            },
            areaName: "WindowArea");
        }
    }
}

결과

 

참고

https://github.com/Federerer/Notifications.Wpf

 

GitHub - Federerer/Notifications.Wpf: Toast notifications for WPF

Toast notifications for WPF. Contribute to Federerer/Notifications.Wpf development by creating an account on GitHub.

github.com

 

728x90
그리드형

'C# > WPF' 카테고리의 다른 글

[WPF] AddHandler 로 KeyDownEvent 처리하기  (0) 2023.04.28
[WPF] Parent Binding  (0) 2023.04.20
[WPF] Popup Drag Move 처리하기  (0) 2023.04.19
[WPF] VectorConverter  (0) 2023.04.18
[WPF] Size3DConverter  (0) 2023.04.18
Posted by kjun
,