9

I am playing with Avalonia and I am trying to show a message box equivalent to WinForms' MessageBox.Show(). I have found the GitHub issue that is requesting an API for that but I am wondering what people are doing in the meantime.

Do we need to implement a window or user control that acts like a message box and show it via ShowDialog?

Sabith
  • 1,628
  • 2
  • 20
  • 38
Stilgar
  • 22,354
  • 14
  • 64
  • 101

2 Answers2

15

MessageBox.xaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="MsgBox.MessageBox" SizeToContent="WidthAndHeight" CanResize="False">
    <StackPanel HorizontalAlignment="Center">
        <TextBlock HorizontalAlignment="Center" Name="Text"/>
        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal" Name="Buttons">
            <StackPanel.Styles>
                <Style Selector="Button">
                    <Setter Property="Margin" Value="5"/>
                </Style>
            </StackPanel.Styles>

        </StackPanel>
    </StackPanel>
</Window>

MessageBox.xaml.cs

using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace MsgBox
{
    class MessageBox : Window
    {
        public enum MessageBoxButtons
        {
            Ok,
            OkCancel,
            YesNo,
            YesNoCancel
        }

        public enum MessageBoxResult
        {
            Ok,
            Cancel,
            Yes,
            No
        }

        public MessageBox()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public static Task<MessageBoxResult> Show(Window parent, string text, string title, MessageBoxButtons buttons)
        {
            var msgbox = new MessageBox()
            {
                Title = title
            };
            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;
                    msgbox.Close();
                };
                buttonPanel.Children.Add(btn);
                if (def)
                    res = r;
            }

            if (buttons == MessageBoxButtons.Ok || buttons == MessageBoxButtons.OkCancel)
                AddButton("Ok", MessageBoxResult.Ok, true);
            if (buttons == MessageBoxButtons.YesNo || buttons == MessageBoxButtons.YesNoCancel)
            {
                AddButton("Yes", MessageBoxResult.Yes);
                AddButton("No", MessageBoxResult.No, true);
            }

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


            var tcs = new TaskCompletionSource<MessageBoxResult>();
            msgbox.Closed += delegate { tcs.TrySetResult(res); };
            if (parent != null)
                msgbox.ShowDialog(parent);
            else msgbox.Show();
            return tcs.Task;
        }


    }

}

Usage:

await  MessageBox.Show(mainWindow, "Test", "Test title", MessageBox.MessageBoxButtons.YesNoCancel)
kekekeks
  • 3,193
  • 19
  • 16
  • Thanks. For some reason the Window is drown with a black square over it and it is only fixed when I maximize and minimize it but I guess it is another bug. Also the window has the standard window buttons but it shouldn't be hard to remove those... I hope. – Stilgar Apr 16 '19 at 21:27
  • Please, file github issues for both of these problems. – kekekeks Apr 17 '19 at 11:29
  • I don't think having the standard buttons is a bug. Seems to me like a Window object is supposed to have its standard buttons by default. I hid them by setting some property about Window decorations to false (Can't check now I'm on another computer). Also the black spot might have been only in debug mode. – Stilgar Apr 18 '19 at 08:58
  • kekekeks Thank you, this is awesome! :D @Stilgar if you put this in your initialization code: this.SystemDecorations = SystemDecorations.BorderOnly; It'll remove the window decorations (and, apparently, the border chrome too...) – CherryCoke Mar 17 '21 at 19:58
6

As of 2022, you can use the MessageBox.Avalonia package. Message boxes created with it have both Show and ShowDialog methods: https://github.com/AvaloniaCommunity/MessageBox.Avalonia/wiki/Api-0.9#ms-classes. The project's wiki has examples for popular use cases.

IhateTrains
  • 61
  • 1
  • 3
  • 3