private void StartService(CancellationToken ct) { Task.Factory.StartNew(() => { try { //获取在屏幕上显示的下一个位置 int nextLocation = FindNextLocation(); if (nextLocation != -1) { while (!ct.IsCancellationRequested && queueMessages.Count > 0) { NotifyMessage notifyMsg = null; if (queueMessages.TryDequeue(out notifyMsg)) { NotifyMessageViewModel viewModel = new NotifyMessageViewModel (notifyMsg, displayLocations[nextLocation], () => { displayMessages[nextLocation] = null; try { NotifyMessageTypeLists.Remove(notifyMsg.ErrorCode); } catch (Exception ex) { } } ); displayMessages[nextLocation] = viewModel; var dispatcher = Application.Current.Dispatcher; dispatcher.Invoke(new Action(() => { NotifyMessageView window = new NotifyMessageView(); window.ShowInTaskbar = false; window.DataContext = viewModel; window.Show(); }), DispatcherPriority.Background); Thread.Sleep(1000); } } Stop(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } ); }
/// <summary> /// 异步显示信息 /// </summary> /// <param name="msg">显示的信息<see cref="NotifyMessage"/></param> public void EnqueueMessage(NotifyMessage msg) { try { if (!NotifyMessageTypeLists.Contains(msg.ErrorCode)) { NotifyMessageTypeLists.Add(msg.ErrorCode); queueMessages.Enqueue(msg); Start(); } } catch (Exception ex) { } }
public NotifyMessageViewModel(NotifyMessage content, AnimateLocation location, Action closeAction) { this._content = content; this._location = location; this._closeAction = closeAction; }