feat: add icon, improve demo.

This commit is contained in:
rabbitism
2024-01-11 22:00:17 +08:00
parent 5eeebb020f
commit ae4323d3fe
8 changed files with 168 additions and 54 deletions

View File

@@ -1,15 +1,23 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
namespace Ursa.Controls;
public static class MessageBox
{
public static async Task<MessageBoxResult> ShowAsync(string message)
public static async Task<MessageBoxResult> ShowAsync(
string message,
string? title = null,
MessageBoxIcon icon = MessageBoxIcon.None,
MessageBoxButton button = MessageBoxButton.OKCancel)
{
var messageWindow = new MessageBoxWindow()
var messageWindow = new MessageBoxWindow(button)
{
Content = message
Content = message,
Title = title,
MessageIcon = icon,
};
var lifetime = Application.Current?.ApplicationLifetime;
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime)
@@ -32,31 +40,20 @@ public static class MessageBox
}
}
public static async Task<MessageBoxResult> ShowAsync(string message, string title, MessageBoxButton button)
public static async Task<MessageBoxResult> ShowAsync(
Window owner,
string message,
string title,
MessageBoxIcon icon = MessageBoxIcon.None,
MessageBoxButton button = MessageBoxButton.OKCancel)
{
var messageWindow = new MessageBoxWindow(button)
{
Content = message,
Title = title
Title = title,
MessageIcon = icon,
};
var lifetime = Application.Current?.ApplicationLifetime;
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime)
{
var main = classLifetime.MainWindow;
if (main is null)
{
messageWindow.Show();
return MessageBoxResult.None;
}
else
{
var result = await messageWindow.ShowDialog<MessageBoxResult>(main);
return result;
}
}
else
{
return MessageBoxResult.None;
}
var result = await messageWindow.ShowDialog<MessageBoxResult>(owner);
return result;
}
}

View File

@@ -0,0 +1,15 @@
namespace Ursa.Controls;
public enum MessageBoxIcon
{
Asterisk, // Same as Information
Error,
Exclamation, // Same as Warning
Hand, // Same as Error
Information,
None,
Question,
Stop, // Same as Error
Warning,
Success,
}

View File

@@ -1,3 +1,4 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
@@ -30,9 +31,13 @@ public class MessageBoxWindow: Window
protected override Type StyleKeyOverride => typeof(MessageBoxWindow);
static MessageBoxWindow()
public static readonly StyledProperty<MessageBoxIcon> MessageIconProperty = AvaloniaProperty.Register<MessageBoxWindow, MessageBoxIcon>(
nameof(MessageIcon));
public MessageBoxIcon MessageIcon
{
get => GetValue(MessageIconProperty);
set => SetValue(MessageIconProperty, value);
}
public MessageBoxWindow()