private void BuildMessageBox( string messageBoxText, string caption, MessageBoxServiceButton button, List<string> customLabels) { var rootVisual = this.FindRootVisual(); if (null != rootVisual) { var bgColor = (Color)Application.Current.Resources["PhoneBackgroundColor"]; var overlayColor = Color.FromArgb(127, bgColor.R, bgColor.G, bgColor.B); var fgColor = (Color)Application.Current.Resources["PhoneForegroundColor"]; var opacityColor = Color.FromArgb(200, bgColor.R, bgColor.G, bgColor.B); this._rootElement = new Grid { Background = new SolidColorBrush(overlayColor) }; this._rootElement.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); this._rootElement.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); this._mbsRoot = new Grid { Background = new SolidColorBrush(overlayColor), Projection = new PlaneProjection() }; this._mbsRoot.ColumnDefinitions.Add( new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); this._mbsRoot.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); this._mbsRoot.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); this._rootElement.Children.Add(this._mbsRoot); // Make sure that our grid spans all rows and columns of it's parent, if the // parent is a Grid. if (rootVisual is Grid) { var parent = (Grid)rootVisual; int columnCount = parent.ColumnDefinitions.Count; if (columnCount > 0) { this._rootElement.SetValue(Grid.ColumnSpanProperty, columnCount); } int rowCount = parent.RowDefinitions.Count; if (rowCount > 0) { this._rootElement.SetValue(Grid.RowSpanProperty, rowCount); } } Border background = new Border { Background = new SolidColorBrush(fgColor) }; background.SetValue(Grid.ColumnSpanProperty, 2); background.SetValue(Grid.RowSpanProperty, 2); this._mbsRoot.Children.Add(background); Border opaqueOverlay = new Border { Background = new SolidColorBrush(opacityColor) }; opaqueOverlay.SetValue(Grid.ColumnSpanProperty, 2); opaqueOverlay.SetValue(Grid.RowSpanProperty, 2); this._mbsRoot.Children.Add(opaqueOverlay); // Add the caption. StackPanel container = new StackPanel { Margin = new Thickness(12, 0, 12, 12) }; container.SetValue(Grid.ColumnSpanProperty, 2); TextBlock title = new TextBlock { FontFamily = new FontFamily("Segoe WP Semibold"), FontSize = 32, Margin = new Thickness(12), Text = caption, TextWrapping = TextWrapping.Wrap }; title.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated); container.Children.Add(title); // Add the message text. TextBlock message = new TextBlock { FontSize = 24, Margin = new Thickness(12, 0, 12, 0), Text = messageBoxText, TextWrapping = TextWrapping.Wrap }; container.Children.Add(message); this._mbsRoot.Children.Add(container); rootVisual.Children.Add(this._rootElement); // Add the required buttons. switch (button) { case MessageBoxServiceButton.OK: this.CreateOKButton( null == customLabels ? MessageBoxServiceStrings.OK : customLabels[0], 2); container.SetValue(Grid.ColumnSpanProperty, 1); break; case MessageBoxServiceButton.OKCancel: this._mbsRoot.ColumnDefinitions.Add( new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); this.CreateOKButton( null == customLabels ? MessageBoxServiceStrings.OK : customLabels[0]); this.CreateCancelButton( null == customLabels ? MessageBoxServiceStrings.Cancel : customLabels[1], 1); break; case MessageBoxServiceButton.YesNoCancel: this._mbsRoot.ColumnDefinitions.Add( new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); this._mbsRoot.ColumnDefinitions.Add( new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); background.SetValue(Grid.ColumnSpanProperty, 3); opaqueOverlay.SetValue(Grid.ColumnSpanProperty, 3); container.SetValue(Grid.ColumnSpanProperty, 3); this.CreateYesButton( null == customLabels ? MessageBoxServiceStrings.Yes : customLabels[0]); this.CreateNoButton( null == customLabels ? MessageBoxServiceStrings.No : customLabels[1], 3); this.CreateCancelButton( null == customLabels ? MessageBoxServiceStrings.Cancel : customLabels[2], 2); break; case MessageBoxServiceButton.YesNo: this._mbsRoot.ColumnDefinitions.Add( new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); this.CreateYesButton( null == customLabels ? MessageBoxServiceStrings.Yes : customLabels[0]); this.CreateNoButton( null == customLabels ? MessageBoxServiceStrings.No : customLabels[1]); break; default: // Not possible to hit this. break; } // Update and start the storyboard to show the message box. foreach (var timeline in this._showStoryboard.Children) { Storyboard.SetTarget(timeline, this._mbsRoot); } // Once the elements are ready, start the storyboard to show them. this._mbsRoot.InvokeOnLayoutUpdated(() => { this._showStoryboard.Begin(); }); } }
/// <summary> /// Displays a message box that contains the specified text, title bar caption, and /// response buttons. /// </summary> /// <param name="messageBoxText">The message to display.</param> /// <param name="caption">The title of the message box.</param> /// <param name="button">A value that indicates the button or buttons to display.</param> /// <param name="customLabels">A list of custom labels in the same order as the buttons to be displayed.</param> /// <exception cref="ArgumentNullException">The <b>messageBoxText</b> or <b>caption</b> /// parameter is null.</exception> /// <exception cref="ArgumentException">The <b>button</b> parameter is not a valid /// <see cref="MessageBoxServiceButton"/> value.</exception> /// <remarks>Handle the <see cref="MessageBoxService.Closed"/> event to determine which /// button the user tapped.</remarks> public void Show( string messageBoxText, string caption = "", MessageBoxServiceButton button = MessageBoxServiceButton.OK, List<string> customLabels = null) { if (null == messageBoxText) { throw new ArgumentNullException("messageBoxText"); } if (null == caption) { throw new ArgumentNullException("caption"); } if (((int)button < (int)MessageBoxServiceButton.OK) || ((int)button > (int)MessageBoxServiceButton.YesNo)) { throw new ArgumentException( "The button property must be a valid value of the MesageBoxServiceButton enumeration", "button"); } // Reset the result. this.Result = MessageBoxResult.None; // Hide the application bar if it's currently visible. if ((null != this.CurrentPage) && (null != this.CurrentPage.ApplicationBar)) { this._appBarIsVisible = this.CurrentPage.ApplicationBar.IsVisible; this.CurrentPage.ApplicationBar.IsVisible = false; } // Hook up the BackKeyPress to close the message box. if (null != this.CurrentPage) { this.CurrentPage.BackKeyPress += this.OnBackKeyPress; } // Build the message box and start the show storyboard. this.BuildMessageBox(messageBoxText, caption, button, customLabels); this.IsOpen = true; }