using BPA.UIControl.Commons; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows; using System.Windows.Data; using static BPA.UIControl.DialogContainer; namespace BPA.UIControl { /// /// 对话框操作类 /// public class Dialog { /// /// 对话框集合 /// internal static Dictionary Dialogs { get; private set; } = new Dictionary(); private static IDialogDataContextConfiguration dataContextConfiguration = new DialogDataContextConfiguration(); /// /// 配置对话框 DataContext 操作逻辑 /// /// 配置逻辑 public static void ConfigureDataContextAction(IDialogDataContextConfiguration configuration) { dataContextConfiguration = configuration; } /// /// 添加对话框 /// /// 标识 /// 对话框 internal static void AddDialogContainer(string identifier, DialogContainer dialog) { if (Dialogs.ContainsKey(identifier)) { _ = Dialogs.Remove(identifier); } Dialogs.Add(identifier, dialog); } private static async Task ShowInternal(DialogContainer dialog, object content, object parameters, string title, Action openHandler, Action closeHandle, bool? showCloseButton) { dialog.Dispatcher.VerifyAccess(); if (!dataContextConfiguration.OnOpenAction(dialog, content, parameters)) { dialog.Title = title; } dialog.DialogContent = content; dialog.IsShowCloseButton = showCloseButton == null ? dialog.IsShowCloseButton : showCloseButton.Value; dialog.BeforeOpenHandler = openHandler; dialog.AfterCloseHandler = closeHandle; DialogContainer.OpenDialogCommand.Execute(parameters, dialog); var taskCompletionSource = new TaskCompletionSource(); dialog.Tag = taskCompletionSource; dialog.AfterClose += OnDialogClosed; var result = await taskCompletionSource.Task; dialog.AfterClose -= OnDialogClosed; dataContextConfiguration.OnCloseAction(); return result; } private static void OnDialogClosed(object sender, DialogResultRoutedEventArgs e) { var dialog = sender as DialogContainer; var taskCompletionSource = dialog.Tag as TaskCompletionSource; taskCompletionSource.TrySetResult(e.Result); dialog.DialogContent = null; dialog.AfterClose -= OnDialogClosed; dialog.Tag = null; } /// /// 显示指定对话框 /// /// DialogContainer 标识 /// 内容 /// 参数 /// 标题 /// 打开前处理程序 /// 关闭后处理程序 /// 是否显示默认关闭按钮 /// 结果 public static async Task Show(string identifier, object content, object parameters = null, string title = null, Action openHandler = null, Action closeHandle = null, bool? showCloseButton = null) { if (Dialogs.TryGetValue(identifier, out DialogContainer container)) { return await ShowInternal(container, content, parameters, title, openHandler, closeHandle, showCloseButton); } throw new NullReferenceException($"The dialog container Identifier '{identifier}' could not be found"); } /// /// 显示指定对话框 /// (默认 BPA.UIControlWindow 下 DialogContainer 容器) /// /// 内容 /// 参数 /// 标题 /// 打开前处理程序 /// 关闭后处理程序 /// 是否显示默认关闭按钮 /// 结果 public static async Task Show(object content, object parameters = null, string title = null, Action openHandler = null, Action closeHandle = null, bool? showCloseButton = null) { var activedWindow = WindowHelper.GetCurrentWindow() ?? throw new NullReferenceException("Can't find the actived window"); DialogContainer container = activedWindow.TryGetChildFromVisualTree(null) ?? throw new NullReferenceException("Can't Find the DialogContainer"); return await ShowInternal(container, content, parameters, title, openHandler, closeHandle, showCloseButton); } /// /// 关闭对话框 /// /// 标识 /// 参数 public static void Close(string identifier, object parameter = null) { if (Dialogs.TryGetValue(identifier, out DialogContainer dialog)) { DialogContainer.CloseDialogCommand.Execute(parameter, dialog); } } /// /// 关闭对话框 /// /// 对话框 /// 参数 public static void Close(DialogContainer dialog, object parameter = null) { DialogContainer.CloseDialogCommand.Execute(parameter, dialog); } /// /// 关闭 /// /// 参数 /// 找不到对话框 public static void Close(object parameter = null) { var activedWindow = WindowHelper.GetCurrentWindow() ?? throw new NullReferenceException("Can't find the actived window"); DialogContainer container = activedWindow.TryGetChildFromVisualTree(null) ?? throw new NullReferenceException("Can't Find the DialogContainer"); Close(container, parameter); } } }