public void AddNotification(Toast toast) { if (!Dispatcher.CheckAccess()) // CheckAccess returns true if you're on the dispatcher thread { Dispatcher.Invoke(new Action(() => AddNotification(toast))); return; } toast.Id = _count++; if (ToastsCollection.Count + 1 > MaxNotifications) _buffer.Add(toast); else ToastsCollection.Add(toast); //Show window if there're notifications if (ToastsCollection.Count > 0 && !IsActive) Show(); }
public void RemoveNotification(Toast toast) { if (!Dispatcher.CheckAccess()) // CheckAccess returns true if you're on the dispatcher thread { Dispatcher.Invoke(new Action(() => RemoveNotification(toast))); return; } if (ToastsCollection.Contains(toast)) ToastsCollection.Remove(toast); if (_buffer.Count > 0) { ToastsCollection.Add(_buffer[0]); _buffer.RemoveAt(0); } //Close window if there's nothing to show if (ToastsCollection.Count < 1) Hide(); }
static Toaster() { ToasterThread = new Thread(() => { Manager = new ToastsManager(); Manager.Show(); Manager.Closed += (sender2, e2) => Manager.Dispatcher.InvokeShutdown(); Images.Load(); //load to this dispatcher Default = new Toast() { Title = null, Image = Images.SuccessThumbs, Message = null, TextColor = Colors.White, SubTitle = "-", BackgroundColor = Colors.Black }; _sync.Set(); System.Windows.Threading.Dispatcher.Run(); }); ToasterThread.Name = "ToasterThread"; ToasterThread.SetApartmentState(ApartmentState.STA); ToasterThread.Start(); }
/// <summary> /// Adds a toast /// </summary> /// <param name="toast"></param> public static void AddToast(Toast toast) { Manager.AddNotification(toast); }