0

I tried to create a toast notification with the custom audio. While the program running, the program can display the toast. I added list of toast on ListBox too. It works smoothly for displaying notification, but it cannot remove the toast from list box.

This is the code for adding toast:

public void Add(ref ListBox display, string value, string value2, TimeSpan occurs)
{
    DateTime when = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
        occurs.Hours, occurs.Minutes, occurs.Seconds);

    if (when > DateTime.Now)
    {
        // Construct the toast content
        ToastContent toastContent = new ToastContent()
        {
            //toast display here
        };

        //audio 

        // And create the toast notification
        ScheduledToastNotification toast = new ScheduledToastNotification(toastContent.GetXml(), when);
        // And then send the toast       
        toast.Id = random.Next(1, 100000000).ToString();
        ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);            
        display.Items.Add(new Item { Id = toast.Id, Content = value, Time = when.ToString() });
    }
}

This is for removing toast from listbox:

public void Remove(ListBox display)
{
    if (display.SelectedIndex > -1)
    {
        ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
        notifier.RemoveFromSchedule(notifier.GetScheduledToastNotifications().Where(
            p => p.Id.Equals(((Item)display.SelectedItem).Id)).SingleOrDefault());
        display.Items.RemoveAt(display.SelectedIndex);
    }
}
6
  • Use find item in the list box and try to remove Commented Nov 23, 2016 at 21:39
  • You said that "it cannot remove the toast from list box". Does that mean that you get an Exception from trying it? Or that the ListBox still shows the toast, despite it being removed? Commented Nov 24, 2016 at 0:24
  • Also, setting the audio to the toast has nothing to do with your problem. Could you please remove that from the code, so we can read it more easily? Commented Nov 24, 2016 at 0:26
  • Sorry, I found my wrong code, so I already resolved it. Thanks ... Commented Nov 24, 2016 at 2:47
  • There's no need to pass the ListBox by ref.
    – Peter Torr
    Commented Nov 24, 2016 at 6:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.