Пример #1
0
        public static Task <MessageBoxResult> ShowAsync(DialogHostControl host, string title, string text, MessageBoxButtons buttons)
        {
            var tcs = new TaskCompletionSource <MessageBoxResult>();

            var msgbox = new MessageBoxControl();

            msgbox.FindControl <TextBlock>("Text").Text = text;
            var buttonPanel = msgbox.FindControl <StackPanel>("Buttons");

            var res = MessageBoxResult.Ok;

            void AddButton(string caption, MessageBoxResult r, bool def = false)
            {
                var btn = new Button {
                    Content = caption
                };

                btn.Click += (_, __) => {
                    res = r;
                    host.CloseDialog();
                    tcs.TrySetResult(res);
                };
                buttonPanel.Children.Add(btn);
                if (def)
                {
                    res = r;
                }
            }

            switch (buttons)
            {
            case MessageBoxButtons.Ok:
            case MessageBoxButtons.OkCancel:
                AddButton("Ok", MessageBoxResult.Ok, true);
                break;

            case MessageBoxButtons.YesNo:
            case MessageBoxButtons.YesNoCancel:
                AddButton("Yes", MessageBoxResult.Yes, true);
                AddButton("No", MessageBoxResult.No);
                break;
            }

            if (buttons == MessageBoxButtons.OkCancel || buttons == MessageBoxButtons.YesNoCancel)
            {
                AddButton("Cancel", MessageBoxResult.Cancel, true);
            }

            host.ShowDialog(msgbox, title);

            return(tcs.Task);
        }
 /// <inheritdoc />
 public Task <MessageBoxResult> ShowAsync(string title, string message, MessageBoxButtons buttons)
 {
     return(MessageBoxControl.ShowAsync(_host, title, message, buttons));
 }