'Toast Notification Progress bar / data binding not working

i copied exactly the code of this example to my C# WPF project: https://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/toast-progress-bar?tabs=builder-syntax

But the last line of SendUpdateableToastWithProgress() - "ToastNotificationManager.CreateToastNotifier().Show(toast);" - throws "System.Exception: 'Element not found. (Exception from HRESULT: 0x80070490)'"

Looks like that transfering ToastContentBuilder content to the ToastNotification using content.getXML() failes. I really can't figure it out - do you have any idea, please? Thanks a lot in advance...

Here is the complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;

namespace References
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SendUpdatableToastWithProgress();
            UpdateProgress();
        }

        public void SendUpdatableToastWithProgress()
        {
            // Define a tag (and optionally a group) to uniquely identify the notification, in order update the notification data later;
            string tag = "weekly-playlist";
            string group = "downloads";

            // Construct the toast content with data bound fields
            var content = new ToastContentBuilder()
                .AddText("Downloading your weekly playlist...")
                .AddVisualChild(new AdaptiveProgressBar()
                {
                    Title = "Weekly playlist",
                    Value = new BindableProgressBarValue("progressValue"),
                    ValueStringOverride = new BindableString("progressValueString"),
                    Status = new BindableString("progressStatus")
                })
                .GetToastContent();

            // Generate the toast notification
            var toast = new ToastNotification(content.GetXml());

            // Assign the tag and group
            toast.Tag = tag;
            toast.Group = group;

            // Assign initial NotificationData values
            // Values must be of type string
            toast.Data = new NotificationData();
            toast.Data.Values["progressValue"] = "0.6";
            toast.Data.Values["progressValueString"] = "15/26 songs";
            toast.Data.Values["progressStatus"] = "Downloading...";

            // Provide sequence number to prevent out-of-order updates, or assign 0 to indicate "always update"
            toast.Data.SequenceNumber = 1;

            // Show the toast notification to the user
            ToastNotificationManager.CreateToastNotifier().Show(toast);
        }

        public void UpdateProgress()
        {
            // Construct a NotificationData object;
            string tag = "weekly-playlist";
            string group = "downloads";

            // Create NotificationData and make sure the sequence number is incremented
            // since last update, or assign 0 for updating regardless of order
            var data = new NotificationData
            {
                SequenceNumber = 2
            };

            // Assign new values
            // Note that you only need to assign values that changed. In this example
            // we don't assign progressStatus since we don't need to change it
            data.Values["progressValue"] = "0.7";
            data.Values["progressValueString"] = "18/26 songs";

            // Update the existing notification's data by using tag/group
            ToastNotificationManager.CreateToastNotifier().Update(data, tag, group);
    }

}
}



Solution 1:[1]

Same issue here!

For those who still looking for solution: use ToastNotificationManagerCompat ("Compat" in the class Name) instead of ToastNotificationManager - and the sample code will work as expected.

I also have found that we can use Show method and do the initialization in the callback too - as follows:

new ToastContentBuilder()
 .AddText("Downloading your weekly playlist...")
 .AddVisualChild(new AdaptiveProgressBar()
 {
     Title = "Weekly playlist",
     Value = new BindableProgressBarValue("progressValue"),
     ValueStringOverride = new BindableString("progressValueString"),
     Status = new BindableString("progressStatus")
  }).Show( toast => {

    // Assign the tag and group
    toast.Tag = tag;
    toast.Group = group;

    // ... the rest of the sample code that initializes the toast goes here

  });

Which looks a bit more elegant than using builder.GetContentToastContent().GetXml() to create instance of ToastNotification as shown in the documentation.

Another thing to note: you can use indeterminate as value for progressValue - it will make progress bar enter the indeterminate state (unknown progress)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1