첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ NotificationModel.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 알림 모델
    /// </summary>
    public class NotificationModel : DependencyObject
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 제목 속성 - CaptionProperty

        /// <summary>
        /// 제목 속성
        /// </summary>
        public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register
        (
            "Caption",
            typeof(string),
            typeof(NotificationModel),
            new PropertyMetadata(string.Empty)
        );

        #endregion
        #region 내용 속성 - ContentProperty

        /// <summary>
        /// 내용 속성
        /// </summary>
        public static readonly DependencyProperty ContentProperty = DependencyProperty.Register
        (
            "Content",
            typeof(string),
            typeof(NotificationModel),
            new PropertyMetadata(string.Empty)
        );

        #endregion
        #region 시간 속성 - TimeProperty

        /// <summary>
        /// 시간 속성
        /// </summary>
        public static readonly DependencyProperty TimeProperty = DependencyProperty.Register
        (
            "Time",
            typeof(string),
            typeof(NotificationModel),
            new PropertyMetadata(string.Empty)
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 제목 - Caption

        /// <summary>
        /// 제목
        /// </summary>
        public string Caption
        {
            get
            {
                return (string)GetValue(CaptionProperty);
            }
            set
            {
                SetValue(CaptionProperty, value);
            }
        }

        #endregion
        #region 내용 - Content

        /// <summary>
        /// 내용
        /// </summary>
        public string Content
        {
            get
            {
                return (string)GetValue(ContentProperty);
            }
            set
            {
                SetValue(ContentProperty, value);
            }
        }

        #endregion
        #region 시간 - Time

        /// <summary>
        /// 시간
        /// </summary>
        public string Time
        {
            get
            {
                return (string)GetValue(TimeProperty);
            }
            set
            {
                SetValue(TimeProperty, value);
            }
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="NotificationService 클래스 : 커스텀 토스트 알림(Toast Notification) 표시하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <DataTemplate x:Key="CustomDataTemplateKey">
            <Border
                BorderThickness="1"
                BorderBrush="Black"
                Background="DarkBlue">
                <StackPanel
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    Orientation="Vertical">
                    <TextBlock
                        Margin="5"
                        HorizontalAlignment="Left"
                        Foreground="White"
                        FontFamily="SegoeUI"
                        FontSize="20"
                        FontWeight="Bold"
                        Text="{Binding Caption}" />
                    <TextBlock
                        Margin="3"
                        HorizontalAlignment="Center"
                        Foreground="LightGray"
                        FontFamily="SegoeUI"
                        FontSize="16"
                        Text="{Binding Content}" />
                    <TextBlock
                        Margin="3"
                        HorizontalAlignment="Right"
                        Foreground="Gray"
                        FontFamily="SegoeUI"
                        FontSize="14"
                        Text="{Binding Time}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Button Name="showToastNotificationButton"
            Width="200"
            Height="30"
            Content="토스트 알림 표시하기" />
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;

using DevExpress.Data;
using DevExpress.Mvvm;
using DevExpress.Mvvm.UI;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 알림 서비스
        /// </summary>
        private NotificationService service;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            DataTemplate template = FindResource("CustomDataTemplateKey") as DataTemplate;

            this.service = new NotificationService();

            this.service.ApplicationId                     = "TestProject";
            this.service.ApplicationName                   = "TestProject";
            this.service.UseWin8NotificationsIfAvailable   = true;
            this.service.CreateApplicationShortcut         = true;
            this.service.CustomNotificationTemplate        = template;
            this.service.CustomNotificationDuration        = TimeSpan.FromSeconds(1);
            this.service.CustomNotificationPosition        = NotificationPosition.BottomRight;
            this.service.CustomNotificationVisibleMaxCount = 10;

            ShellHelper.TryCreateShortcut(this.service.ApplicationId, this.service.ApplicationName);

            Closing                                += Window_Closing;
            this.showToastNotificationButton.Click += showToastNotificationButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 닫을 경우 처리하기 - Window_Closing(sender, e)

        /// <summary>
        /// 윈도우 닫을 경우 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            ShellHelper.TryRemoveShortcut(this.service.ApplicationName);
        }

        #endregion
        #region 토스트 알림 표시하기 버튼 클릭시 처리하기 - showToastNotificationButton_Click(sender, e)

        /// <summary>
        /// 토스트 알림 표시하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void showToastNotificationButton_Click(object sender, RoutedEventArgs e)
        {
            NotificationModel model = new NotificationModel();

            model.Caption = "알림";
            model.Content = "등록된 사용자가 없습니다.";
            model.Time    = $"시각 : {DateTime.Now}";

            INotification notification = this.service.CreateCustomNotification(model);

            notification.ShowAsync().ContinueWith(resultTask => ProcessToastNotificationClicked(resultTask, null));
        }

        #endregion
        #region 토스트 알림 클릭시 처리하기 - ProcessToastNotificationClicked(resultTask, parameter)

        /// <summary>
        /// 토스트 알림 클릭시 처리하기
        /// </summary>
        /// <param name="resultTask">결과 태스크</param>
        /// <param name="parameter">매개 변수</param>
        private void ProcessToastNotificationClicked(Task<NotificationResult> resultTask, object parameter)
        {
            NotificationResult result = resultTask.Result;

            switch(result)
            {
                case NotificationResult.Activated :

                    Process.Start("https://icodebroker.tistory.com");

                    break;
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요