private static void OnIdentifierChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { MessageBoxContainer container = d as MessageBoxContainer; string identify = e.NewValue.ToString(); if (Containers.ContainsKey(identify)) { Containers.Remove(identify); } container.Background = new SolidColorBrush(Colors.Transparent); container.Visibility = Visibility.Hidden; Containers.Add(identify, container); }
public static async Task <MessageBoxResult> ShowInContainer(string containerIdentifier, string message, string title = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxIcon icon = MessageBoxIcon.None) { MessageBoxResult result = MessageBoxResult.No; bool isReturnResult = false; if (!MessageBoxContainer.Containers.ContainsKey(containerIdentifier)) { return(result); } MessageBoxContainer container = MessageBoxContainer.Containers[containerIdentifier]; container.Dispatcher.VerifyAccess(); MessageBoxCard card = new MessageBoxCard { Content = message, Title = title, MessageBoxButton = button, }; card.ReturnResult += (a, b) => { isReturnResult = true; result = b.Result; }; switch (icon) { case MessageBoxIcon.None: card.IsShowIcon = false; break; case MessageBoxIcon.Info: card.IsShowIcon = true; card.IconType = IconType.InformationFill; card.ThemeColorBrush = infoBrush; break; case MessageBoxIcon.Success: card.IsShowIcon = true; card.IconType = IconType.CheckboxCircleFill; card.ThemeColorBrush = successBrush; break; case MessageBoxIcon.Warining: card.IsShowIcon = true; card.IconType = IconType.ErrorWarningFill; card.ThemeColorBrush = warningBrush; break; case MessageBoxIcon.Error: card.IsShowIcon = true; card.IconType = IconType.CloseCircleFill; card.ThemeColorBrush = errorBrush; break; case MessageBoxIcon.Question: card.IsShowIcon = true; card.IconType = IconType.QuestionFill; card.ThemeColorBrush = questionBrush; break; } container.Visibility = Visibility.Visible; // 动画 Storyboard cardStoryboard = new Storyboard(); DoubleAnimation opacityAnimation = new DoubleAnimation { From = 0, To = 1, Duration = new Duration(TimeSpan.FromMilliseconds(200)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty)); DoubleAnimation transformAnimation = new DoubleAnimation { From = 50, To = 0, Duration = new Duration(TimeSpan.FromMilliseconds(200)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); cardStoryboard.Children.Add(opacityAnimation); cardStoryboard.Children.Add(transformAnimation); // 背景动画 ColorAnimation backgroundAnimation = new ColorAnimation { From = Colors.Transparent, To = containerBackgroun, Duration = new Duration(TimeSpan.FromMilliseconds(150)), EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } }; backgroundAnimation.Completed += (sender, e) => { container.Child = card; card.BeginStoryboard(cardStoryboard); card.Focus(); }; container.Background.BeginAnimation(SolidColorBrush.ColorProperty, backgroundAnimation); await Task.Run(() => { while (!isReturnResult) { Thread.Sleep(10); } }); return(result); }