-
Notifications
You must be signed in to change notification settings - Fork 22
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.
- 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.
- 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
NotificationConfigurationobject.
<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>`
- If you are using MVVM pattern you need to do the following steps:
- Inject
INotificationDialogServiceinto your viewModel. - Create
MailNotificationobject. - 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).
- Inject
notice that: when any property of the configuration object set to null it will use the default value except displayDuration property must be
TimeSpan.Zeroto use the default value.
- If you are using Code-Behind you need to do the following steps:
- Create
INotificationDialogServiceobject. - Create
MailNotificationobject. - 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).
- Create