2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
2025-07-11 09:20:23 +08:00
|
|
|
|
using WPFluent.Controls;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
using MessageBoxButton = System.Windows.MessageBoxButton;
|
|
|
|
|
|
using MessageBoxResult = System.Windows.MessageBoxResult;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WPFluent.Controls;
|
|
|
|
|
|
|
|
|
|
|
|
public static partial class MessageBox
|
|
|
|
|
|
{
|
|
|
|
|
|
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
Show(null!, messageBoxText, caption, button, icon, defaultResult);
|
|
|
|
|
|
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
Show(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
Show(owner, messageBoxText, caption, button, icon.ToGlyph(), defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
Show(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30, FontFamily = new FontFamily(new Uri("pack://application:,,,/WPFluent;component/Resources/Fonts/Segoe Fluent Icons.ttf"), "Segoe Fluent Icons") }, defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon, System.Windows.MessageBoxResult defaultResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
owner
|
|
|
|
|
|
??= Application.Current.Windows.OfType<Window>().FirstOrDefault(window => window.IsActive)
|
|
|
|
|
|
?? Application.Current.MainWindow;
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher dispatcher = owner?.Dispatcher ?? Application.Current.Dispatcher;
|
|
|
|
|
|
|
|
|
|
|
|
return dispatcher.Invoke((Func<System.Windows.MessageBoxResult>)(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBoxDialog window = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Owner = owner,
|
|
|
|
|
|
IconSource = icon,
|
|
|
|
|
|
MessageBoxIcon = icon.ToMessageBoxIcon(),
|
|
|
|
|
|
Result = defaultResult,
|
|
|
|
|
|
Content = messageBoxText,
|
|
|
|
|
|
MessageBoxButtons = button,
|
|
|
|
|
|
Caption = caption ?? string.Empty,
|
|
|
|
|
|
Title = caption ?? string.Empty,
|
|
|
|
|
|
ResizeMode = ResizeMode.NoResize,
|
|
|
|
|
|
WindowStyle = WindowStyle.SingleBorderWindow,
|
|
|
|
|
|
WindowStartupLocation = owner is null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return window.ShowDialog();
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
ShowAsync(null!, messageBoxText, caption, button, icon, defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
ShowAsync(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
ShowAsync(owner, messageBoxText, caption, button, icon.ToGlyph(), defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon, System.Windows.MessageBoxResult defaultResult) =>
|
|
|
|
|
|
ShowAsync(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30, FontFamily = new FontFamily(new Uri("pack://application:,,,/WPFluent;component/Resources/Fonts/Segoe Fluent Icons.ttf"), "Segoe Fluent Icons") }, defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon, System.Windows.MessageBoxResult defaultResult)
|
|
|
|
|
|
{
|
|
|
|
|
|
TaskCompletionSource<System.Windows.MessageBoxResult> taskSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
|
|
|
|
|
|
|
owner
|
|
|
|
|
|
??= Application.Current.Windows.OfType<Window>().FirstOrDefault(window => window.IsActive)
|
|
|
|
|
|
?? Application.Current.MainWindow;
|
|
|
|
|
|
|
|
|
|
|
|
Dispatcher dispatcher = owner?.Dispatcher ?? Application.Current.Dispatcher;
|
|
|
|
|
|
|
|
|
|
|
|
dispatcher.Invoke(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
System.Windows.MessageBoxResult result = Show(owner!, messageBoxText, caption, button, icon, defaultResult);
|
|
|
|
|
|
|
|
|
|
|
|
taskSource.TrySetResult(result);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return taskSource.Task;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|