Toast Notification in Windows Forms

Introduction

Have you ever wanted to add  the modern tost notifications in your windows form project  In this blog post, we will explore how to do  this using code snippets.

To display a Windows 10 or Windows 11 toast notification in a C# Windows Form application, you can make use of the Windows.UI library. Here are the steps to create and display a toast notification.

  1. Add a reference to the Windows.UI library in your project.
  2. Create a ToastContent object, which will contain the text and images to be displayed in the toast notification.
  3. Create a ToastNotification object, which will contain the ToastContent object and other notification options.
  4. Use the ToastNotificationManager class to display the ToastNotification object.

Here's an example of how to display a simple toast notification in a C# Windows Form application.

using Windows.UI.Notifications;

// Create the ToastContent object
ToastContent toastContent = new ToastContentBuilder()
    .AddText("Hello, world!")
    .GetToastContent();

// Create the ToastNotification object
ToastNotification toastNotification = new ToastNotification(toastContent.GetXml());

// Display the ToastNotification object
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
```

In this example, the toast notification will display the text "Hello, world!". You can customize the toast notification by adding more text, images, and notification options to the ToastContent object. You can also listen for events such as the user clicking on the toast notification or dismissing it.

Notification Demo

Note. that this code will only work on Windows 10 or Windows 11. If you need to display toast notifications on other.