_______ _ _ _ _ _ __ _ _ _ ___
|__ __| | | | \ | | | | (_)/ _(_) | | (_) |__ \
| | ___ __ _ ___| |_| \| | ___ | |_ _| |_ _ ___ __ _| |_ _ ___ _ __ ___ __ __ ) |
| |/ _ \ / _` / __| __| . ` |/ _ \| __| | _| |/ __/ _` | __| |/ _ \| '_ \/ __| \ \ / // /
| | (_) | (_| \__ \ |_| |\ | (_) | |_| | | | | (_| (_| | |_| | (_) | | | \__ \ \ V // /_
|_|\___/ \__,_|___/\__|_| \_|\___/ \__|_|_| |_|\___\__,_|\__|_|\___/|_| |_|___/ \_/|____|
Install-Package ToastNotifications
using ToastNotifications;
using ToastNotifications.Lifetime;
using ToastNotifications.Position;
/* * */
Notifier notifier = new Notifier(cfg =>
{
cfg.LifetimeSupervisor = new TimeAndCountBasedLifetimeSupervisor(TimeSpan.FromSeconds(5), MaximumNotificationCount.FromCount(15));
cfg.PositionProvider = new PrimaryScreenPositionProvider(Corner.BottomRight, 10, 10);
});
This class will contains all data needed to display in notification in this example its just a "Title" property but it can be anything even commands for buttons. CustomNotification derrive from "NotificationBase" which is base class for all notifications in ToastNotifications v2. Every notification is composed of two things:
- Its data - properties used for data binding
- DisplayPart - which describes how notification looks and display its data
- Create class called CustomNotification
- Derrive it from NotificationBase and INotifyPropertyChanged
- Add notification data using properties
- Create DisplayPart property as shown below
using System.ComponentModel;
using System.Runtime.CompilerServices;
using ToastNotifications.Core;
public class CustomNotification : NotificationBase, INotifyPropertyChanged
{
private CustomDisplayPart _displayPart;
public override NotificationDisplayPart DisplayPart => _displayPart ?? (_displayPart = new CustomDisplayPart(this));
public CustomNotification(string message)
{
Message = message;
}
private string _message;
public string Message
{
get
{
return _message;
}
set
{
_message = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
DisplayPart describes notification appearance, its nothing more than just costom UI control derrivied from NotificationDisplayPart. The simpliest way to create new DisplayPart is by creating WPF "UserControl" in Visual Studio.
- Create UserControl called CustomDisplayPart
- Change the base class from UserControl to NotificationDisplayPart in CustomDisplayPart.xaml and CustomDisplayPart.xaml.cs
- Create constructor parameter of type CustomNotification to connect DisplayPart with notification data
- Bind UI controls to notification data
CustomDisplayPart.xaml
<core:NotificationDisplayPart x:Class="CustomNotificationsExample.CustomMessage.CustomDisplayPart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CustomNotificationsExample.CustomMessage"
xmlns:core="clr-namespace:ToastNotifications.Core;assembly=ToastNotifications"
mc:Ignorable="d" Background="Gray"
d:DesignHeight="60" d:DesignWidth="250"
d:DataContext="{d:DesignInstance local:CustomNotification, IsDesignTimeCreatable=False}" >
<Grid Margin="5">
<TextBlock Text="{Binding Message}" FontWeight="Light" Foreground="White" Grid.Row="1" TextWrapping="Wrap" />
</Grid>
</core:NotificationDisplayPart>
CustomDisplayPart.xaml.cs
public partial class CustomDisplayPart : NotificationDisplayPart
{
public CustomDisplayPart(CustomNotification customNotification)
{
InitializeComponent();
Bind(customNotification);
}
}
Extension method will be used to simplify the usage of CustomNotification in applicaiton code
public static class CustomMessageExtensions
{
public static void ShowCustomMessage(this Notifier notifier, string message)
{
notifier.Notify<CustomNotification>(() => new CustomNotification(message));
}
}
notifier.ShowCustomMessage("This is custom notification based on user control");