using XFEWarframe.Desktop.Editing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Windows.Storage.Pickers; using WinRT.Interop; using XFEExtension.NetCore.WinUIHelper.Implements.Services; using XFEExtension.NetCore.WinUIHelper.Interface.Services; using XFEExtension.NetCore.WinUIHelper.Utilities; using XFEWarframe.Admin.Interface.Services; namespace XFEWarframe.Admin.Implements.Services; public sealed class InteractionService : GlobalServiceBase, IInteractionService { private readonly IDialogService dialogs = ServiceManager.GetService(); private readonly SemaphoreSlim dialogGate = new(1, 1); public async Task ConfirmAsync(string title, string message) => await ShowAsync(new ContentDialog { Title = title, Content = new TextBlock { Text = message, TextWrapping = TextWrapping.Wrap }, PrimaryButtonText = UiText.Translate("确认"), CloseButtonText = UiText.Translate("取消"), DefaultButton = ContentDialogButton.Close }) == ContentDialogResult.Primary; public async Task ShowAsync(ContentDialog dialog) { await dialogGate.WaitAsync(App.Lifetime); try { App.Lifetime.ThrowIfCancellationRequested(); dialog.Name = "ActiveDialog"; dialog.XamlRoot = ((FrameworkElement)App.MainWindow.Content).XamlRoot; dialog.RequestedTheme = ((FrameworkElement)App.MainWindow.Content).ActualTheme; dialogs.DialogDictionary[dialog.Name] = dialog; using var cancellation = App.Lifetime.Register(() => App.MainWindow.DispatcherQueue.TryEnqueue(dialog.Hide)); return await dialogs.ShowDialog(dialog.Name); } finally { dialogs.DialogDictionary.Remove("ActiveDialog"); dialogGate.Release(); } } public async Task PickFolderAsync() { var picker = new FolderPicker(); picker.FileTypeFilter.Add("*"); InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow)); return (await picker.PickSingleFolderAsync())?.Path; } public async Task OpenJsonAsync() { var picker = new FileOpenPicker(); picker.FileTypeFilter.Add(".json"); InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow)); return (await picker.PickSingleFileAsync())?.Path; } public async Task SaveJsonAsync(string suggestedName) { var picker = new FileSavePicker { SuggestedFileName = suggestedName }; picker.FileTypeChoices.Add(UiText.Translate("JSON 文件"), new List { ".json" }); InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(App.MainWindow)); return (await picker.PickSaveFileAsync())?.Path; } }