public static void Show(string containerIdentifier, MessageType type, UIElement element, int millisecondTimeOut = 3000, bool isClearable = true) { if (!MessageContainer.Containers.ContainsKey(containerIdentifier)) { return; } if (millisecondTimeOut <= 0) { isClearable = true; } Panel messagePanel = MessageContainer.Containers[containerIdentifier]; messagePanel.Dispatcher.VerifyAccess(); MessageCard messageCard; switch (type) { default: case MessageType.None: messageCard = new MessageCard { Content = element, IsClearable = isClearable }; break; case MessageType.Info: messageCard = new MessageCard { Content = element, IsClearable = isClearable, Style = infoStyle }; break; case MessageType.Warning: messageCard = new MessageCard { Content = element, IsClearable = isClearable, Style = warningStyle }; break; case MessageType.Success: messageCard = new MessageCard { Content = element, IsClearable = isClearable, Style = successStyle }; break; case MessageType.Error: messageCard = new MessageCard { Content = element, IsClearable = isClearable, Style = errorStyle }; break; } messagePanel.Children.Add(messageCard); // 进入动画 Storyboard enterStoryboard = new Storyboard(); DoubleAnimation opacityAnimation = new DoubleAnimation { From = 0, To = 1, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty)); DoubleAnimation transformAnimation = new DoubleAnimation { From = -30, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); enterStoryboard.Children.Add(opacityAnimation); enterStoryboard.Children.Add(transformAnimation); // 退出动画 Storyboard exitStoryboard = new Storyboard(); DoubleAnimation exitOpacityAnimation = new DoubleAnimation { From = 1, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitOpacityAnimation, new PropertyPath(UIElement.OpacityProperty)); DoubleAnimation exitTransformAnimation = new DoubleAnimation { From = 0, To = -30, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitTransformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); exitStoryboard.Children.Add(exitOpacityAnimation); exitStoryboard.Children.Add(exitTransformAnimation); if (millisecondTimeOut > 0) { // 进入动画完成 enterStoryboard.Completed += async(sender, e) => { await Task.Run(() => { Thread.Sleep(millisecondTimeOut); }); messageCard.BeginStoryboard(exitStoryboard); }; } // 退出动画完成 exitStoryboard.Completed += (sender, e) => { messagePanel.Children.Remove(messageCard); }; messageCard.BeginStoryboard(enterStoryboard); }
public void AddMessageCard(MessageCard messageCard, int millisecondTimeOut) { messageCard.Close += MessageCard_Close; messageStackPanel.Children.Add(messageCard); // 进入动画 Storyboard enterStoryboard = new Storyboard(); DoubleAnimation opacityAnimation = new DoubleAnimation { From = 0, To = 1, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(OpacityProperty)); DoubleAnimation transformAnimation = new DoubleAnimation { From = -30, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); enterStoryboard.Children.Add(opacityAnimation); enterStoryboard.Children.Add(transformAnimation); // 退出动画 Storyboard exitStoryboard = new Storyboard(); DoubleAnimation exitOpacityAnimation = new DoubleAnimation { From = 1, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitOpacityAnimation, new PropertyPath(OpacityProperty)); DoubleAnimation exitTransformAnimation = new DoubleAnimation { From = 0, To = -30, Duration = new Duration(TimeSpan.FromMilliseconds(300)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn } }; Storyboard.SetTargetProperty(exitTransformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); exitStoryboard.Children.Add(exitOpacityAnimation); exitStoryboard.Children.Add(exitTransformAnimation); // 进入动画完成 if (millisecondTimeOut > 0) { enterStoryboard.Completed += async(sender, e) => { await Task.Run(() => { Thread.Sleep(millisecondTimeOut); }); Dispatcher.Invoke(() => { messageCard.BeginStoryboard(exitStoryboard); }); }; } // 退出动画完成 exitStoryboard.Completed += (sender, e) => { Dispatcher.Invoke(() => { messageStackPanel.Children.Remove(messageCard); if (messageStackPanel.Children.Count == 0) { this.Close(); } }); }; messageCard.BeginStoryboard(enterStoryboard); }