How I can attach click
event to a button at system toast notifications on WPF application using Microsoft.Toolkit.Uwp.Notifications library?
I know that I need to use IBackground
interface but I can't tie to this class, because the following code is causing the error ( ToastNotificationActionTrigger()
element not found, HRESULT: 0x80070490) .
private void RegisterBackgroundTask()
{
const string taskName = "ToastBackgroundTask";
// Otherwise create the background task
var builder = new BackgroundTaskBuilder();
builder.Name = taskName;
builder.TaskEntryPoint = typeof(ToastNotificationBackgroundTask).FullName;
// And set the toast action trigger
builder.SetTrigger(new ToastNotificationActionTrigger());
// And register the task
builder.Register();
}
The view of my notification:
Please, help.
I used notifications in WPF a year ago, here is how I achieved it:
First you have to add from Nuget the package Windows API Code Pack.
Then I used the following code:
public static void RaiseGeneratedNotification(int errors, int warnings)
{
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("Web Studio"));
stringElements[1].AppendChild(toastXml.CreateTextNode(Strings.Errors+": "+errors));
stringElements[2].AppendChild(toastXml.CreateTextNode(Strings.Warnings+": " +warnings));
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("App.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml)
{
ExpirationTime = DateTimeOffset.Now.AddMinutes(2)
};
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(Resources.AppId).Show(toast);
}
With this code I did the following steps:
You also have to register an AppId in your application shortcut.
I hope this can help you.
User contributions licensed under CC BY-SA 3.0