System Notifications at WPF

2

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:

View of my notification

Please, help.

c#
wpf
notifications
asked on Stack Overflow Mar 9, 2017 by Vitalii Kulyk • edited Mar 9, 2017 by ganchito55

1 Answer

0

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:

  1. Get the notification template.
  2. Get the fields of the notification
  3. Fill the fields
  4. Create a notification with the template
  5. Raise the notification

You also have to register an AppId in your application shortcut.

I hope this can help you.

answered on Stack Overflow Mar 9, 2017 by ganchito55

User contributions licensed under CC BY-SA 3.0