/// <summary> /// 有新的通知消息到达 /// </summary> /// <param name="notifyMsgModel"></param> private void OnNewMsgReached(NotifyMsgModel notifyMsgModel) { //判断计时器是否正在运行 //如果没有运行代表当前没有显示消息,则直接显示否则加入缓存 if (!_displayingMsg) { DisplayMsg(notifyMsgModel); } else { //当前正在处理信息显示,则直接加到队尾 lock (_msgs) { _msgs.Enqueue(notifyMsgModel); } } }
private void NotifyNewMsg(object obj) { if (NewMsgReached != null) { for (int i = 0; i < 5; i++) { var newNotifyMsgModel = new NotifyMsgModel { Id = _mockIndex, Title = string.Format("第{0}条消息,{1} ", _mockIndex, DateTime.Now) + Guid.NewGuid() + Guid.NewGuid(), Content = Guid.NewGuid().ToString("N") }; _mockIndex++; NewMsgReached(newNotifyMsgModel); } } }
public NotifyMsgItem(NotifyMsgModel notifyMsgModel) { NotifyMsgModel = notifyMsgModel; InitializeComponent(); }
private async void DisplayMsg(NotifyMsgModel msg) { if (msg != null) { _displayingMsg = true; //展示信息 DispatcherHelper.CheckBeginInvokeOnUI(() => { var item = new NotifyMsgItem(msg) { HorizontalAlignment = HorizontalAlignment.Center, Opacity = 1, Margin = new Thickness(0, ActualHeight + 10, 0, 0) }; _container.Children.Add(item); //向上滑动动画总显示时间减1秒 var floatAnimation = new ThicknessAnimation(); floatAnimation.From = new Thickness(0, ActualHeight + 10, 0, 0); floatAnimation.To = new Thickness(0); floatAnimation.Duration = new Duration(TimeSpan.FromSeconds(_displaySeconds - 1)); item.BeginAnimation(MarginProperty, floatAnimation); }); await Task.Delay(TimeSpan.FromSeconds(_delayNextMsgSecond)); //判断缓存中是否还有待显示信息 NotifyMsgModel nextMsg = null; lock (_msgs) { if (_msgs.Count > 0) { nextMsg = _msgs.Dequeue(); } } if (nextMsg != null) { DisplayMsg(nextMsg); } else { _displayingMsg = false; } await Task.Delay(TimeSpan.FromSeconds(_displaySeconds - _delayNextMsgSecond - 1)); DispatcherHelper.CheckBeginInvokeOnUI(() => { var item = _container.Children[0]; //显示完成,隐藏动画耗时1秒 var opacityAnimation = new DoubleAnimation(); opacityAnimation.From = 1; opacityAnimation.To = 0; opacityAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); item.BeginAnimation(OpacityProperty, opacityAnimation); }); //等待主线程完成 await Task.Delay(TimeSpan.FromSeconds(1)); DispatcherHelper.CheckBeginInvokeOnUI(() => { var item = _container.Children[0]; _container.Children.Remove(item); }); } }