using System.Threading; using AIK.Common.Enum; using AIK.Common.IconString; using AIK.Common.Interface; using AIK.Common.Language; using AIK.Views.MenuPages.CommonPage; using AIK.Views.UIViewModels; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; namespace AIK.Views.UIServices { public class ModalDialogService : IModalDialogService { private readonly IServiceProvider _serviceProvider; // 通过构造函数注入 IServiceProvider public ModalDialogService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public async Task ShowDialogAsync(string title = "请稍候", string message = "正在处理...", MessageTypeEnum messageType = MessageTypeEnum.Info, bool canCancel = true, bool isAutoClose = true, int closeTimeSpan = 3000) { title = title == "请稍候" ? AiKLanguage.LanguageKey.GetValueOrDefault("DialogPleaseWait") ?? string.Empty : title; message = message == "正在处理..." ? AiKLanguage.LanguageKey.GetValueOrDefault("DialogProcessing") ?? string.Empty : message; var vm = new DialogMessageViewModel(title, message, messageType, canCancel); var window = new DialogWindow { DataContext = vm, Owner = Application.Current.MainWindow // 可选:设为模态父窗 }; // 启动后台任务(不在 UI 线程阻塞) _ = Task.Run(async () => { try { // 切回 UI 线程更新消息并关闭 await Application.Current.Dispatcher.InvokeAsync(async () => { vm.Title = title; vm.Message = message; vm.MessageType = messageType; if (isAutoClose) { var closets = TimeSpan.FromMilliseconds(closeTimeSpan).TotalSeconds; vm.Message += $"\r\n{closets}秒自动关闭"; await Task.Delay(closeTimeSpan); window.Close(); } }); } catch (OperationCanceledException) { await Application.Current.Dispatcher.InvokeAsync(() => { vm.Message = AiKLanguage.LanguageKey.GetValueOrDefault("DialogCancelled") ?? string.Empty; window.Close(); }); } catch (Exception ex) { await Application.Current.Dispatcher.InvokeAsync(async () => { vm.Message = $"{AiKLanguage.LanguageKey.GetValueOrDefault("DialogErrorPrefix") ?? string.Empty}: {ex.Message}"; //await Task.Delay(1500); window.Close(); }); } }, vm.CancellationToken); // 显示模态窗口(阻塞调用方直到关闭) window.ShowDialog(); } public async Task ShowProgressAsync(Func, CancellationToken, Task<(bool, string)>> work, string title = "请稍候", string message = "正在烧录...", bool canCancel = true) { title = title == "请稍候" ? AiKLanguage.LanguageKey.GetValueOrDefault("DialogPleaseWait") ?? string.Empty : title; var vm = new ProgressDialogViewModel(title, message, canCancel); var window = new ProgressDialogWindow { DataContext = vm, Owner = Application.Current.MainWindow // 可选:设为模态父窗 }; Stopwatch stopwatch = Stopwatch.StartNew(); var timer = new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Background, (s, e) => { vm.TimeTotalText = stopwatch.Elapsed.ToString(@"mm\:ss\.ff"); }, Application.Current.Dispatcher); // 启动后台任务(不在 UI 线程阻塞) _ = Task.Run(async () => { try { var progress = new Progress(p => { // Progress 回调可能在后台线程,需调度到 UI Application.Current.Dispatcher.Invoke(() => vm.ReportProgress(p)); }); var (success, resultMessage) = await work(progress, vm.CancellationToken); // 切回 UI 线程更新消息并关闭 await Application.Current.Dispatcher.InvokeAsync(async () => { vm.Message = resultMessage; if (success) { var ts = stopwatch.Elapsed; vm.TimeTotalText = $"{AiKLanguage.LanguageKey.GetValueOrDefault("DialogElapsedPrefix") ?? string.Empty}-{ts.Minutes:D2}:{ts.Seconds:D2}.{ts.Milliseconds.ToString().PadLeft(3, '0').Substring(0, 2)}"; } vm.IsCompleted = true; }); } catch (OperationCanceledException) { await Application.Current.Dispatcher.InvokeAsync(() => { vm.Message = AiKLanguage.LanguageKey.GetValueOrDefault("DialogCancelled") ?? string.Empty; vm.IsCompleted = true; }); } catch (Exception ex) { await Application.Current.Dispatcher.InvokeAsync(async () => { vm.Message = $"{AiKLanguage.LanguageKey.GetValueOrDefault("DialogErrorPrefix") ?? string.Empty}: {ex.Message}"; vm.IsCompleted = true; }); } finally { stopwatch.Stop(); timer.Stop(); } }, vm.CancellationToken); // 显示模态窗口(阻塞调用方直到关闭) window.ShowDialog(); } public async Task ShowTipsDialogAsync(string message = "", MessageTypeEnum typeEnum = MessageTypeEnum.Info, bool canCancel = true, bool isAutoClose = true, int closeTime = 3000) { LightTipWindow.ShowTip(Application.Current.MainWindow, AiKLanguage.LanguageKey.GetValueOrDefault("UpToDate")); } public async Task ShowQRCodeDialogAsync(IServiceProvider serviceProvider, string title = "请稍候", string message = "正在处理...", MessageTypeEnum messageType = MessageTypeEnum.Info, bool canCancel = true, bool isAutoClose = true, int closeTimeSpan = 3000) { title = title == "请稍候" ? AiKLanguage.LanguageKey.GetValueOrDefault("DialogPleaseWait") ?? string.Empty : title; message = message == "正在处理..." ? AiKLanguage.LanguageKey.GetValueOrDefault("DialogProcessing") ?? string.Empty : message; var vm = QRCodeMessageViewModel.Create( _serviceProvider, // 👈 传入容器 title, message, messageType, canCancel ); var window = new QRCodeDialogWindow { DataContext = vm, // 手动绑定 ViewModel Owner = Application.Current.MainWindow }; // 启动后台任务(不在 UI 线程阻塞) _ = Task.Run(async () => { try { } catch (OperationCanceledException) { await Application.Current.Dispatcher.InvokeAsync(() => { vm.Message = AiKLanguage.LanguageKey.GetValueOrDefault("DialogCancelled") ?? string.Empty; window.Close(); }); } catch (Exception ex) { await Application.Current.Dispatcher.InvokeAsync(async () => { vm.Message = $"{AiKLanguage.LanguageKey.GetValueOrDefault("DialogErrorPrefix") ?? string.Empty}: {ex.Message}"; //await Task.Delay(1500); window.Close(); }); } }, vm.CancellationToken); // 显示模态窗口(阻塞调用方直到关闭) window.ShowDialog(); } } }