Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 82 additions & 13 deletions wpf/Toast-Notification/Customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ SfToastNotification.Show(this, new ToastOptions
Message = "Your document has been saved.",
Actions = new List<ToastAction>
{
new ToastAction
{
Label = "Undo",
},
new ToastAction
{
Label = "OK",
CloseOnClick = true
}
new ToastAction("Undo"),
new ToastAction("OK")
}
});

Expand All @@ -52,19 +45,18 @@ private void UndoLastSave()

SfToastNotification.Show(this, new ToastOptions
{
Mode = ToastMode.Screen,
Title = "New Message",
Message = "You have a new message.",
Actions = new List<ToastAction>
{
new ToastAction
new ToastAction("Reply")
{
Label = "Reply",
Callback = () => OpenReplyWindow(),
CloseOnClick = true
},
new ToastAction
new ToastAction("Later")
{
Label = "Later",
CloseOnClick = true
}
}
Expand All @@ -78,6 +70,83 @@ private void OpenReplyWindow()
{% endhighlight %}
{% endtabs %}


## Handling Action Button Click Events in System Toast Notifications

The **SfToastNotification** control supports system toast notifications with interaction support, allowing you to add action buttons and handle click events using Windows notification APIs.

### Prerequisites

To handle system toast action button click events, install the following package:

- **CommunityToolkit.WinUI.Notifications**

This package is required only for **handling activation events** (such as button clicks) from system toast notifications.

### .NET Framework Projects

In .NET Framework WPF applications, system toast action button click events can be handled by registering for the [ToastNotificationManagerCompat.OnActivated](https://learn.microsoft.com/en-us/windows/apps/develop/notifications/app-notifications/send-local-toast?tabs=desktop#tabpanel_1_desktop) event.

{% tabs %}
{% highlight C# %}

SfToastNotification.Show(this, new ToastOptions
{
Mode = ToastMode.Default,
Title = "File Saved",
Message = "Your document has been saved.",
Actions = new List<ToastAction>
{
new ToastAction("Open", "action=open"),
new ToastAction("Dismiss", "action=close")
}
});

// Register activation handler
ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated;

private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
{
ToastArguments arguments = ToastArguments.Parse(e.Argument);

if (arguments.TryGetValue("action", out string action))
{
switch (action)
{
case "Open":
// Handle Open action
MessageBox.Show("Open action clicked");
break;

case "Dismiss":
// Handle Dismiss action
MessageBox.Show("Dismiss action clicked");
break;
}
}
}

{% endhighlight %}
{% endtabs %}

---

### .NET Core / .NET (Windows) Projects

For .NET Core and modern .NET projects, system toast notifications require the [Target Framework Moniker (TFM)](https://learn.microsoft.com/en-us/windows/apps/develop/notifications/app-notifications/send-local-toast?tabs=desktop#step-1-install-nuget-package) to include a **minimum Windows OS version** to access the toast notification APIs.

{% tabs %}
{% highlight XML %}

<TargetFramework>net8.0-windows10.0.19041.0.0</TargetFramework>

{% endhighlight %}
{% endtabs %}

Once configured, action buttons and click event handling work the same way as in .NET Framework projects.

N> View [Sample](https://github.com/SyncfusionExamples/SystemToast-ActionButton-Handling) in GitHub

## Customizing Action Buttons with ActionTemplate

Each action button in a toast notification can be individually customized using the `ActionTemplate` property available in the `ToastAction` class. When defining actions within a toast, you can optionally specify a custom data template to control the appearance and behavior of each action button.
Expand Down