
MessageBox for AvaloniaUI
MessageBox is a quite often used form for various graphical frameworks, but for some reason she did not find a place in avalonia, then we will give her life on her own.

A ready-made solution, which I plan to provide support and update, and I will be glad if anyone joins, can be found on nuget and gitlab .
My goal is to get closer to the standard Windows MsgBox, at least in terms of experience.
So:
Call
and we get a ready-made window with a heading and text content. (Windows 10, Ubuntu 18.04)


The MessageBox class contains 3 methods:
ShowForResult - returns the result of pressing the keys
ShowDialog - returns the result and makes the MessageBox dialog for the selected parent window
Show - displays the window ignoring the result of pressing the
API keys
A window is created inside any method,
which installs the content.
The content in this case is the Grid, which includes two lines: the first for the text field:
second for nested grid with buttons:
The functionality of the buttons is set by the method:
The method accepts a window that the buttons will manipulate, and the result returned by them.
And, the last thing to consider is a piece of code that provides the result of a button click:
As a result, we get various simple windows with buttons, which will allow us to create cross-platform MessageBox:

UPD
A method has been added that tries to call the implementation of native windows, if it does not cope, it calls the windows presented above.
Special thanks to the user worldbeater .

A ready-made solution, which I plan to provide support and update, and I will be glad if anyone joins, can be found on nuget and gitlab .
My goal is to get closer to the standard Windows MsgBox, at least in terms of experience.
So:
Call
MessageBox.ShowForResult("test","Wanna test smth?",MessageBox.MessageBoxButtons.OkCancel);
and we get a ready-made window with a heading and text content. (Windows 10, Ubuntu 18.04)


The MessageBox class contains 3 methods:
ShowForResult - returns the result of pressing the keys
ShowDialog - returns the result and makes the MessageBox dialog for the selected parent window
Show - displays the window ignoring the result of pressing the
API keys
A window is created inside any method,
var messageBox = new MessageBox();
which installs the content.
messageBox.Content = CreateBaseMsgBox(text, buttons, messageBox);
The content in this case is the Grid, which includes two lines: the first for the text field:
var textBlock = new TextBlock();
textBlock.Text = text;
textBlock.TextAlignment = TextAlignment.Center;
textBlock.TextWrapping = TextWrapping.Wrap;
Grid.SetRow(textBlock,0);
grid.Children.Add(textBlock);
second for nested grid with buttons:
var btnGrid = GetButtonGrid(GetButton(window, MessageBoxResult.Yes),
GetButton(window,MessageBoxResult.No));
Grid.SetRow(btnGrid,1);
grid.Children.Add(btnGrid);
Full example of the GetButtonGrid method.
This dynamic approach allows you to add an unlimited number of buttons and expand the capabilities of MessageBox without major changes.
private static Grid GetButtonGrid(params Button[] buttons)
{
var grid = new Grid();
List definitions = new List();
for (int i = 0; i < buttons.Length; i++)
{
definitions.Add(new ColumnDefinition{Width = new GridLength(5)});
definitions.Add(new ColumnDefinition{Width = new GridLength(1,GridUnitType.Star)});
}
definitions.Add(new ColumnDefinition{Width = new GridLength(5)});
grid.ColumnDefinitions.AddRange(definitions);
var j = 1;
foreach (var btn in buttons)
{
Grid.SetColumn(btn,j);
j += 2;
grid.Children.Add(btn);
}
return grid;
}
This dynamic approach allows you to add an unlimited number of buttons and expand the capabilities of MessageBox without major changes.
The functionality of the buttons is set by the method:
GetButton(MessageBox window,MessageBoxResult result)
More details
private static Button GetButton(MessageBox window,MessageBoxResult result)
{
var btn = new Button();
btn.Content = result.ToString();
btn.Click += (_, __) =>
{
window.Res = result;
window.Close();
};
return btn;
}
The method accepts a window that the buttons will manipulate, and the result returned by them.
And, the last thing to consider is a piece of code that provides the result of a button click:
var tcs = new TaskCompletionSource();
messageBox.Closed += delegate { tcs.TrySetResult(messageBox.Res); };
...
return tcs.Task;
As a result, we get various simple windows with buttons, which will allow us to create cross-platform MessageBox:

UPD
A method has been added that tries to call the implementation of native windows, if it does not cope, it calls the windows presented above.
Special thanks to the user worldbeater .