优化更新代码,添加界面功能并整合
This commit is contained in:
193
WPFluent/Controls/MessageBox/MessageBox.cs
Normal file
193
WPFluent/Controls/MessageBox/MessageBox.cs
Normal file
@@ -0,0 +1,193 @@
|
||||
|
||||
using WPFluent.Controls;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
using System.Windows.Threading;
|
||||
|
||||
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) =>
|
||||
Show(messageBoxText, null!);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption) =>
|
||||
Show(null!, messageBoxText, caption);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button) =>
|
||||
Show(null!, messageBoxText, caption, button);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon) =>
|
||||
Show(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon) =>
|
||||
Show(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon) =>
|
||||
Show(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon) =>
|
||||
Show(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
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(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon, System.Windows.MessageBoxResult defaultResult) =>
|
||||
Show(null!, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon, System.Windows.MessageBoxResult defaultResult) =>
|
||||
Show(null!, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon, System.Windows.MessageBoxResult defaultResult) =>
|
||||
Show(null!, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText) =>
|
||||
Show(owner, messageBoxText, null!);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption) =>
|
||||
Show(owner, messageBoxText, caption, System.Windows.MessageBoxButton.OK);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button) =>
|
||||
Show(owner, messageBoxText, caption, button, default(IconSource)!);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon) =>
|
||||
Show(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon) =>
|
||||
Show(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon) =>
|
||||
Show(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static System.Windows.MessageBoxResult Show(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon) =>
|
||||
Show(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
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) =>
|
||||
ShowAsync(messageBoxText, null!);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption) =>
|
||||
ShowAsync(null!, messageBoxText, caption);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText) =>
|
||||
ShowAsync(owner, messageBoxText, null!);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button) =>
|
||||
ShowAsync(null!, messageBoxText, caption, button);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption) =>
|
||||
ShowAsync(owner, messageBoxText, caption, System.Windows.MessageBoxButton.OK);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxImage icon) =>
|
||||
ShowAsync(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon) =>
|
||||
ShowAsync(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon) =>
|
||||
ShowAsync(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon) =>
|
||||
ShowAsync(messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button) =>
|
||||
ShowAsync(owner, messageBoxText, caption, button, default(IconSource)!);
|
||||
|
||||
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(string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon, System.Windows.MessageBoxResult defaultResult) =>
|
||||
ShowAsync(null!, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon, System.Windows.MessageBoxResult defaultResult) =>
|
||||
ShowAsync(null!, messageBoxText, caption, button, icon, defaultResult);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource 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) =>
|
||||
ShowAsync(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, MessageBoxSymbolGlyph icon) =>
|
||||
ShowAsync(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, string icon) =>
|
||||
ShowAsync(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
public static Task<System.Windows.MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, System.Windows.MessageBoxButton button, IconSource icon) =>
|
||||
ShowAsync(owner, messageBoxText, caption, button, icon, System.Windows.MessageBoxResult.None);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user