Skip to content

Create custom notification

mohamed magdy edited this page Dec 17, 2016 · 4 revisions

Step by step guide to show and create custom WPF toast notification.

1) Adding the Notification assembly

  • Get the latest WPFToastNotification release and unzip its contents to disk or install the WPFNotification package using NuGet.
  • Open Visual Studio and open an existing WPF application or create a new WPF Application project.
  • Add an assembly reference to WPFNotification.dll
  • Modify the App.xaml and add the following resource dictionary reference:

` <Application.Resources>

 <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="/WPFNotification;component/Assets/NotificationUI.xaml" />
         </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

</Application.Resources>`

notice that : If you install WPFNotification package using NuGet, Automatically it will add the assembly reference and modify App.xaml for you.

2) Creating Mail Notification

  • We need to Create 3 files
    • Custom Notification Model e.g. MailNotification class.
    • Custom Notification UI e.g. NotificationItem.xaml user control and bind it on MailNotification properties.
    • Resource dictionary file e.g. MailNotificationUI.xaml and add dataTemplate to define the presentation of your notification data, you must give it name e.g. x:Key="MailNotificationTemplate" we will need this name when creating the NotificationConfiguration object.

<DataTemplate x:Key="MailNotificationTemplate" DataType="{x:Type Model:MailNotification}"> <NotificationView:NotificationItem/> </DataTemplate>

  • Modify App.xaml and add the MailNotificationUI.xaml resource dictionary reference:

` <Application.Resources>

 <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="/WPFNotification;component/Assets/NotificationUI.xaml" />
             <ResourceDictionary Source="/MailNotificationUI.xaml" />     
         </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

</Application.Resources>`

3) Display Mail Notification

  • If you are using MVVM pattern you need to do the following steps:
    • Inject INotificationDialogService into your viewModel.
    • Create MailNotification object.
    • Create NotificationConfiguration object with TemplateName equal to the name of your dataTemplate.
      var configuration = new NotificationConfiguration(TimeSpan.Zero, null, null, "MailNotificationTemplate");
    • Finally call the show method with notification object and configuration object NotificationDialogServiceObject.ShowNotificationWindow(MailNotificationObject,configurationObject).

notice that: when any property of the configuration object set to null it will use the default value except displayDuration property must be TimeSpan.Zero to use the default value.

  • If you are using Code-Behind you need to do the following steps:
    • Create INotificationDialogService object.
    • Create MailNotification object.
    • Create NotificationConfiguration object with TemplateName equal to the name of your dataTemplate.
      var configuration = new NotificationConfiguration(TimeSpan.Zero, null, null, "MailNotificationTemplate");
    • Finally call the show method with notification object and configuration object NotificationDialogServiceObject.ShowNotificationWindow(MailNotificationObject,configurationObject).

Clone this wiki locally