[chore:] added messagebox
This commit is contained in:
18
MessageBox.axaml
Normal file
18
MessageBox.axaml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<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" SizeToContent="WidthAndHeight"
|
||||||
|
x:Class="Logof_Client.MessageBox"
|
||||||
|
Title="MessageBox">
|
||||||
|
<StackPanel>
|
||||||
|
<TextBlock Name="Text" Margin="10" TextWrapping="Wrap" />
|
||||||
|
<StackPanel HorizontalAlignment="Right" Margin="5" Orientation="Horizontal" Name="Buttons">
|
||||||
|
<StackPanel.Styles>
|
||||||
|
<Style Selector="Button">
|
||||||
|
<Setter Property="Margin" Value="5" />
|
||||||
|
</Style>
|
||||||
|
</StackPanel.Styles>
|
||||||
|
</StackPanel>
|
||||||
|
</StackPanel>
|
||||||
|
</Window>
|
||||||
85
MessageBox.axaml.cs
Normal file
85
MessageBox.axaml.cs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
|
||||||
|
namespace Logof_Client;
|
||||||
|
|
||||||
|
public class MessageBox : Window
|
||||||
|
{
|
||||||
|
public MessageBox()
|
||||||
|
{
|
||||||
|
AvaloniaXamlLoader.Load(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<MessageBoxResult> Show(Window parent, string text, string title,
|
||||||
|
MessageBoxButton buttons = MessageBoxButton.Ok)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
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 == MessageBoxButton.Ok || buttons == MessageBoxButton.OkCancel)
|
||||||
|
AddButton("Ok", MessageBoxResult.Ok, true);
|
||||||
|
if (buttons == MessageBoxButton.YesNo || buttons == MessageBoxButton.YesNoCancel)
|
||||||
|
{
|
||||||
|
AddButton("Yes", MessageBoxResult.Yes);
|
||||||
|
AddButton("No", MessageBoxResult.No, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttons == MessageBoxButton.OkCancel || buttons == MessageBoxButton.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;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error while showing messagebox: " + ex.Message);
|
||||||
|
return Task.FromResult(MessageBoxResult.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum MessageBoxButton
|
||||||
|
{
|
||||||
|
Ok,
|
||||||
|
OkCancel,
|
||||||
|
YesNo,
|
||||||
|
YesNoCancel
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum MessageBoxResult
|
||||||
|
{
|
||||||
|
Ok,
|
||||||
|
Cancel,
|
||||||
|
Yes,
|
||||||
|
No,
|
||||||
|
Error
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user