Beispiel #1
0
        public static Task<MessageBoxResult> ShowAsync(
            string header,
            string content,
            MessageBoxButton button = MessageBoxButton.OK,
            MessageDialogType type = MessageDialogType.Light,
            System.Windows.Window owner = null)
        {
            TaskCompletionSource<MessageBoxResult> taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();

            if (owner == null)
            {
                owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
            }

            MessageDialog messageDialog = new MessageDialog()
            {
                Content = content
            };

            OverlayWindow window = new OverlayWindow()
            {
                Content = messageDialog,
                Owner = owner
            };

            switch (type)
            {
                case MessageDialogType.Accent:
                    window.Style = (Style)window.FindResource("AccentOverlayWindowStyle");
                    break;
                case MessageDialogType.Dark:
                    window.Style = (Style)window.FindResource("DarkOverlayWindowStyle");
                    break;
            }

            messageDialog.Header = new TextBlock()
            {
                Style = (Style)window.FindResource("HeaderTextStyle"),
                Text = header
            };

            if ((button == MessageBoxButton.OK) ||
                (button == MessageBoxButton.OKCancel))
            {
                messageDialog.Items.Add(
                    new MessageDialogButton()
                    {
                        Command = new DelegateCommand(
                            () =>
                            {
                                if (!taskCompletionSource.Task.IsCompleted)
                                {
                                    window.Close();
                                    taskCompletionSource.SetResult(MessageBoxResult.OK);
                                }
                            }),
                        Content = "Ok"
                    });
            }

            if ((button == MessageBoxButton.YesNo) ||
                (button == MessageBoxButton.YesNoCancel))
            {
                messageDialog.Items.Add(
                    new MessageDialogButton()
                    {
                        Command = new DelegateCommand(
                            () =>
                            {
                                if (!taskCompletionSource.Task.IsCompleted)
                                {
                                    window.Close();
                                    taskCompletionSource.SetResult(MessageBoxResult.Yes);
                                }
                            }),
                        Content = "Yes"
                    });
                messageDialog.Items.Add(
                    new MessageDialogButton()
                    {
                        Command = new DelegateCommand(
                            () =>
                            {
                                if (!taskCompletionSource.Task.IsCompleted)
                                {
                                    window.Close();
                                    taskCompletionSource.SetResult(MessageBoxResult.No);
                                }
                            }),
                        Content = "No"
                    });
            }

            if ((button == MessageBoxButton.OKCancel) ||
                (button == MessageBoxButton.YesNoCancel))
            {
                messageDialog.Items.Add(
                    new MessageDialogButton()
                    {
                        Command = new DelegateCommand(
                            () =>
                            {
                                if (!taskCompletionSource.Task.IsCompleted)
                                {
                                    window.Close();
                                    taskCompletionSource.SetResult(MessageBoxResult.Cancel);
                                }
                            }),
                        Content = "Cancel"
                    });
            }

            messageDialog.CancelCommandIndex = messageDialog.Items.Count - 1;
            window.Show();

            return taskCompletionSource.Task;
        }
Beispiel #2
0
        public static Task<MessageBoxResult> ShowAsync(
            string header,
            string content,
            IEnumerable<MessageDialogButton> buttons,
            MessageDialogType type = MessageDialogType.Light,
            System.Windows.Window owner = null,
            int defaultCommandIndex = 0,
            int cancelCommandIndex = -1)
        {
            TaskCompletionSource<MessageBoxResult> taskCompletionSource = new TaskCompletionSource<MessageBoxResult>();

            if (owner == null)
            {
                owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
            }

            MessageDialog messageDialog = new MessageDialog()
            {
                Header = header,
                Content = content
            };

            OverlayWindow window = new OverlayWindow()
            {
                Content = messageDialog,
                Owner = owner
            };

            switch (type)
            {
                case MessageDialogType.Accent:
                    window.Style = (Style)window.FindResource("AccentOverlayWindowStyle");
                    break;
                case MessageDialogType.Dark:
                    window.Style = (Style)window.FindResource("DarkOverlayWindowStyle");
                    break;
            }

            foreach (MessageDialogButton button in buttons)
            {
                messageDialog.Items.Add(button);
            }

            if (cancelCommandIndex == -1)
            {
                messageDialog.CancelCommandIndex = messageDialog.Items.Count - 1;
            }

            window.Show();

            messageDialog
                .FindVisualChildren<Button>()
                .ToList()
                .ForEach(
                    x => x.Click += (sender, e) =>
                    {
                        if (!taskCompletionSource.Task.IsCompleted)
                        {
                            window.Close();
                            taskCompletionSource.SetResult(MessageBoxResult.None);
                        }
                    });

            return taskCompletionSource.Task;
        }