Skip to content

Toast Notifications

Sergei Calabonga edited this page Jun 29, 2025 · 2 revisions

How To

Toast Notification implemented: Success, Information, Warning, Error. How it works? It's easy.

  1. Inject INotificationManager into your ViewModel constructor:
    public partial class MainWindowsViewModel : ViewModelBase, IDisposable
    {
        private readonly INotificationManager _notificationManager;
        
        public MainWindowsViewModel(INotificationManager notificationManager)
        {
            _notificationManager = notificationManager;
        }
        
        // ... other code...
    }
  2. Create a toast message:
        var errorToast = NotificationManager.CreateErrorToast("Message text", "Title");
        //  or
        var successToast = NotificationManager.CreateSuccessToast("Message text", "Title");
        // or 
        var warningToast = NotificationManager.CreateWarningToast("Message text", "Title");
        // or 
        var informationToast = NotificationManager.CreateInformationToast("Message text", "Title");
  3. The show a toast:
     _notificationManager.Show(errorToast);
     // or
     _notificationManager.Show(successToast);
     // or
     _notificationManager.Show(warningToast);
     // or
     _notificationManager.Show(informationToast);
  4. There are two zone where you can show a toasts:
    • Screen - All toast notifications will show on the screen outside of the application. In this case you do not do anything. This case already works "out of the box".

    • NotificationZone - All toast notification will show in the special control NotificationZone you should add on your XAML (VisualTree). You can do this something like shown below:

      Add namespace first

          xmlns:controls="clr-namespace:Calabonga.Commandex.Engine.ToastNotifications.Controls;assembly=Calabonga.Commandex.Engine"

      Then add control NotificationZone with name "NotificationZone":

      <controls:NotificationZone x:Name="NotificationZone" ItemsCountMax="3" Position="TopRight" />

      After than you should add a NotificationZone as a parameter for Show() method:

      _notificationManager.Show(errorToast, "NotificationZone");
      // or
      _notificationManager.Show(successToast, "NotificationZone");
      // or
      _notificationManager.Show(warningToast , "NotificationZone");
      // or
      _notificationManager.Show(informationToast, "NotificationZone");

Known issues

There are many windows created when you are use Toast Notifications. That's why when you close main window of the application the process not exit. Please use this solution to close you application correctly.

In MainWindow of your App add this:

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    Application.Current.Shutdown();
}

Clone this wiki locally