/// <summary>
        /// Display the CustomMessageDialog on screen with the specified parameter from the constructor
        /// </summary>
        public void ShowDialog()
        {
            // Create the base canvas that will hold all the controls and application bar functions
            DialogWindow = new RadModalWindow()
            {
                Name                       = "CustomMessageDialog",
                Background                 = new SolidColorBrush(Color.FromArgb(155, 31, 31, 31)),
                WindowSizeMode             = WindowSizeMode.FitToPlacementTarget,
                VerticalContentAlignment   = VerticalAlignment.Top,                             // Message dialog dispays top of screen
                HorizontalContentAlignment = HorizontalAlignment.Stretch,
                IsClosedOnOutsideTap       = false,                                             // Only close on back button press or button tap
                OpenAnimation              = AnimationService.GetOpenMessageDialogAnimation(),
                CloseAnimation             = AnimationService.GetCloseMessageDialogAnimation(), // need to slide out nicely
            };

            DialogWindow.WindowOpening += (sender, args) => DialogService.DialogOpening(args);
            DialogWindow.WindowClosed  += (sender, args) => DialogService.DialogClosed();

            // Create a Grid to populate with UI controls
            var mainGrid = new Grid()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment   = VerticalAlignment.Top,
                RowDefinitions      =
                {
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Optional Image row
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Title row
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Message row
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Response control(s) row
                },
                Margin = new Thickness(24, 0, 24, 0)
            };


            if (_image != null)
            {
                // Add image to the view
                mainGrid.Children.Add(_image);
                Grid.SetRow(_image, 0);
            }

            // Create title label
            var title = new TextBlock()
            {
                Text                = _title.ToUpper(), // The specified title string in uppercase always
                FontFamily          = new FontFamily("Segoe WP Semibold"),
                FontSize            = Convert.ToDouble(Application.Current.Resources["PhoneFontSizeMedium"]),
                Foreground          = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]),
                Margin              = _image == null ? new Thickness(0, 24, 0, 0) : new Thickness(0),
                HorizontalAlignment = HorizontalAlignment.Left,
                TextWrapping        = TextWrapping.Wrap
            };

            // Add title to the view
            mainGrid.Children.Add(title);
            Grid.SetRow(title, 1);

            // Create message label
            var message = new TextBlock()
            {
                Text                = _message, // The specified message
                FontFamily          = new FontFamily("Segoe WP SemiLight"),
                FontSize            = Convert.ToDouble(Application.Current.Resources["PhoneFontSizeMediumLarge"]),
                Foreground          = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]),
                Margin              = _image == null ? new Thickness(0, 50, 0, 48) : new Thickness(0, 36, 0, 36),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                TextWrapping        = TextWrapping.Wrap // Needed for long strings / texts
            };

            // Add message to the view
            mainGrid.Children.Add(message);
            Grid.SetRow(message, 2);

            // Create response controls panel
            var buttonGrid = new Grid()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(-12, 0, -12, 24)
            };

            // Add response controls to the view
            mainGrid.Children.Add(buttonGrid);
            Grid.SetRow(buttonGrid, 3);

            // Create the response control buttons
            foreach (var dialogButton in _buttons)
            {
                var button = new Button()
                {
                    Content             = dialogButton.Text.ToLower(), // always lower case, modern ui design guideline
                    HorizontalAlignment = HorizontalAlignment.Stretch
                };

                // Focus the button(s) to let the virtual keyboard slide down when it was open
                button.Loaded += (sender, args) => button.Focus();

                var dlgButton = dialogButton;
                button.Tap += (sender, args) =>
                {
                    if (dlgButton.TapAction != null)
                    {
                        // Invoke action after the close animation of the dialog has finished
                        // Used for nice effect of sliding out and see the action
                        DialogWindow.CloseAnimation.Ended += (o, eventArgs) =>
                        {
                            dlgButton.TapAction.Invoke();
                        };

                        // If tap action is defined the dialog result is a custom result
                        this.DialogResult = MessageDialogResult.Custom;

                        // Set result for ShowDialogAsync invocation
                        if (_taskCompletionSource != null)
                        {
                            _taskCompletionSource.TrySetResult(this.DialogResult);
                        }

                        DialogWindow.IsOpen = false;
                    }
                    else
                    {
                        switch (dlgButton.Type)
                        {
                        case MessageDialogButton.Ok:
                        case MessageDialogButton.Yes:
                            this.DialogResult = MessageDialogResult.OkYes;
                            // Set result for ShowDialogAsync invocation
                            if (_taskCompletionSource != null)
                            {
                                _taskCompletionSource.TrySetResult(this.DialogResult);
                            }
                            OnOkOrYesButtonTapped(new EventArgs());
                            break;

                        case MessageDialogButton.Cancel:
                        case MessageDialogButton.No:
                            this.DialogResult = MessageDialogResult.CancelNo;
                            // Set result for ShowDialogAsync invocation
                            if (_taskCompletionSource != null)
                            {
                                _taskCompletionSource.TrySetResult(this.DialogResult);
                            }
                            OnCancelOrNoButtonTapped(new EventArgs());
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        // Set result for ShowDialogAsync invocation
                        if (_taskCompletionSource != null)
                        {
                            _taskCompletionSource.TrySetResult(this.DialogResult);
                        }
                    }
                };

                buttonGrid.Children.Add(button);

                switch (_buttonOrientation)
                {
                case Orientation.Vertical:
                    buttonGrid.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = new GridLength(1, GridUnitType.Star)
                    });
                    Grid.SetRow(button, buttonGrid.RowDefinitions.Count - 1);
                    break;

                case Orientation.Horizontal:
                    buttonGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(1, GridUnitType.Star)
                    });
                    Grid.SetColumn(button, buttonGrid.ColumnDefinitions.Count - 1);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // Used to color the dialog content backgroud
            var border = new Border
            {
                Background          = new SolidColorBrush((Color)Application.Current.Resources["PhoneChromeColor"]),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Child = mainGrid,
            };

            // Set the content on the canvas and show the dialog
            DialogWindow.Content = border;
            DialogWindow.IsOpen  = true;
        }
Example #2
0
        /// <summary>
        /// Display the CustomInputDialog on screen with the specified parameter from the constructor
        /// </summary>
        public void ShowDialog()
        {
            // Create the base canvas that will hold all the controls and application bar functions
            DialogWindow = new RadModalWindow()
            {
                Name = "CustomInputDialog",
                ApplicationBarInfo = new ApplicationBarInfo()
                {
                    Buttons =
                    {
                        CreateOkButton(),     // Default ok button with check icon
                        CreateCancelButton(), // Default cancel button with cross icon
                    }
                },
                Background                 = new SolidColorBrush((Color)Application.Current.Resources["PhoneChromeColor"]),
                IsFullScreen               = true,
                WindowSizeMode             = WindowSizeMode.FitToPlacementTarget,
                VerticalContentAlignment   = VerticalAlignment.Stretch,
                HorizontalContentAlignment = HorizontalAlignment.Stretch,
                IsClosedOnOutsideTap       = false, // Only close on back button press or cancel button tap
                OpenAnimation              = AnimationService.GetOpenDialogAnimation(),
            };

            // When the open animation of the dialog has ended, select a text selection if specified
            // Do this after the animation because else the dialog will be pushed to far up by the default
            // behavior of the focus action on a textbox by the WP OS when keyboard slides in
            DialogWindow.OpenAnimation.Ended += (sender, args) =>
            {
                // If specified make a default text selection so the user can do his action more quickly
                if (_settings.SelectDefaultText)
                {
                    SetTextSelection(InputControl, _settings.DefaultText, _settings.IgnoreExtensionInSelection);
                }

                // Focus to make the selection visible and push up the keyboard immediately
                InputControl.Focus();
            };

            DialogWindow.WindowOpening += (sender, args) => DialogService.DialogOpening(args);
            DialogWindow.WindowClosed  += (sender, args) => DialogService.DialogClosed();

            // Create a Grid to populate with UI controls
            var mainGrid = new Grid()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment   = VerticalAlignment.Stretch,
                RowDefinitions      =
                {
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Title row
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Message row
                    new RowDefinition()
                    {
                        Height = GridLength.Auto
                    },                                               // Input control row
                },
                Margin = new Thickness(24, 0, 24, 0)
            };

            // Create title label
            var title = new TextBlock()
            {
                Text                = _title.ToUpper(), // The specified title string in uppercase always
                FontFamily          = new FontFamily("Segoe WP Semibold"),
                FontSize            = Convert.ToDouble(Application.Current.Resources["PhoneFontSizeMediumLarge"]),
                Foreground          = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]),
                Margin              = new Thickness(0, 28, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                TextWrapping        = TextWrapping.Wrap
            };

            // Add title to the view
            mainGrid.Children.Add(title);
            Grid.SetRow(title, 0);

            // Create message label
            var message = new TextBlock()
            {
                Text                = _message, // The specified message
                FontFamily          = new FontFamily("Segoe WP SemiLight"),
                FontSize            = Convert.ToDouble(Application.Current.Resources["PhoneFontSizeSmall"]),
                Foreground          = new SolidColorBrush((Color)Application.Current.Resources["PhoneSubtleColor"]),
                Margin              = new Thickness(0, 50, 0, 22),
                HorizontalAlignment = HorizontalAlignment.Left,
                TextWrapping        = TextWrapping.Wrap
            };

            // Add message to the view
            mainGrid.Children.Add(message);
            Grid.SetRow(message, 1);

            // Create input control
            InputControl = new TextBox()
            {
                Text = _settings.DefaultText, // The specified default text in the textbox control input area
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(-12)   // Compensate for WP default padding
            };

            // Add input to the view
            mainGrid.Children.Add(InputControl);
            Grid.SetRow(InputControl, 2);

            // Set the content on the canvas and show the dialog
            DialogWindow.Content = mainGrid;
            DialogWindow.IsOpen  = true;
        }