Dispenser/DispenserCommon/Utils/WindowUtil.cs

62 lines
1.7 KiB
C#

using Avalonia.Controls;
namespace DispenserCommon.Utils;
public class WindowUtil
{
private static Window? _mainWindow;
public static void SetMainWindow(Window window)
{
_mainWindow = window;
}
/// <summary>
/// 显示弹窗
/// </summary>
/// <param name="dialog">弹窗</param>
/// <typeparam name="TD">弹窗类型</typeparam>
public static void ShowDialog<TD>(TD dialog) where TD : Window
{
if (_mainWindow == null) return;
dialog.ShowDialog(_mainWindow);
}
/// <summary>
/// 显示弹窗
/// </summary>
/// <param name="dialog">弹窗</param>
/// <param name="action">弹窗回调</param>
/// <typeparam name="TD">弹窗类型</typeparam>
public static void ShowDialog<TD>(TD dialog, Action<TD> action) where TD : Window
{
if (_mainWindow == null) return;
dialog.ShowDialog(_mainWindow);
action(dialog);
}
/// <summary>
/// 显示弹窗
/// </summary>
/// <param name="dialog">弹窗实体</param>
/// <typeparam name="TD">弹窗类型</typeparam>
/// <typeparam name="TR">弹窗回调信息</typeparam>
/// <returns></returns>
public static async Task<TR> ShowDialog<TD, TR>(TD dialog) where TD : Window
{
if (_mainWindow == null) return default;
return await dialog.ShowDialog<TR>(_mainWindow);
}
/// <summary>
/// 无返回值的弹窗
/// </summary>
/// <typeparam name="TD"></typeparam>
public static void ShowDialog<TD>() where TD : Window
{
if (_mainWindow == null) return;
var dialog = Activator.CreateInstance<TD>();
dialog.ShowDialog(_mainWindow);
}
}