diff --git a/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs b/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs index 0718ad590..320df52cc 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs +++ b/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs @@ -213,12 +213,38 @@ namespace System.Application.UI public static async void SetClipboardText(string s) => await Current.Clipboard.SetTextAsync(s); - public Window? MainWindow { get; set; } + Window? mMainWindow; + + public Window MainWindow + { + get + { + Window? mainWindow = null; + + if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + mainWindow = desktop.MainWindow; + } + + if (mainWindow == null) + { + mainWindow = mMainWindow ?? throw new ArgumentNullException(nameof(mainWindow)); + } + + return mainWindow; + } + set + { + mMainWindow = value; + } + } public void ShowChildWindow(object window) { if (window is Window w) + { w.ShowDialog(MainWindow); + } } /// diff --git a/System.Application.SteamTools.Client.Desktop.Avalonia/Application/Services/Implementation/MessageWindowServiceImpl.cs b/System.Application.SteamTools.Client.Desktop.Avalonia/Application/Services/Implementation/MessageWindowServiceImpl.cs index b9c6a4516..887731e25 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia/Application/Services/Implementation/MessageWindowServiceImpl.cs +++ b/System.Application.SteamTools.Client.Desktop.Avalonia/Application/Services/Implementation/MessageWindowServiceImpl.cs @@ -1,6 +1,7 @@ using Avalonia.Controls; using Avalonia.Threading; using System.Application.UI; +using System.Application.UI.ViewModels; using System.Application.UI.ViewModels.Windows; using System.Application.UI.Windows; using System.Threading.Tasks; @@ -11,7 +12,7 @@ namespace System.Application.Services.Implementation { public Task ShowDialog(string messageBoxText) { - return ShowDialog(messageBoxText, "", false); + return ShowDialog(messageBoxText, "", false); } public Task ShowDialog(string messageBoxText, string caption) @@ -21,7 +22,7 @@ namespace System.Application.Services.Implementation public Task ShowDialog(string messageBoxText, string caption, bool isCancelcBtn) { - return MainThreadDesktop.InvokeOnMainThreadAsync(() => + return MainThreadDesktop.InvokeOnMainThreadAsync(() => { var dialog = new MessageWindowViewModel { @@ -34,5 +35,26 @@ namespace System.Application.Services.Implementation return dialog.DialogResult; }); } + + static Type GetWindowType(IMessageWindowService.CustomWindow customWindow) => customWindow switch + { + IMessageWindowService.CustomWindow.MessageBox => typeof(MessageWindowViewModel), + _ => throw new ArgumentOutOfRangeException(nameof(customWindow), customWindow, null), + }; + + public Task Show(IMessageWindowService.CustomWindow customWindow, ViewModelBase? dataContext = null) + { + return MainThreadDesktop.InvokeOnMainThreadAsync(() => + { + var windowType = GetWindowType(customWindow); + var window = (Window)Activator.CreateInstance(windowType); + if (dataContext != null) + { + window.DataContext = dataContext; + } + window.WindowStartupLocation = WindowStartupLocation.CenterScreen; + AppHelper.Current.ShowChildWindow(window); + }); + } } } \ No newline at end of file diff --git a/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs b/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs index 333178935..02e172d01 100644 --- a/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs +++ b/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Application.UI.ViewModels; +using System.Threading.Tasks; using System.Windows; namespace System.Application.Services @@ -28,5 +29,16 @@ namespace System.Application.Services /// 一个 值,用于指定要显示哪个按钮或哪些按钮。 /// 一个 值,用于指定用户单击了哪个消息框按钮。 Task ShowDialog(string messageBoxText, string caption, bool isCancelcBtn); + + Task Show(CustomWindow window, ViewModelBase? dataContext = null); + + public enum CustomWindow : byte + { + MessageBox, + + XXWindow, + + XXXWindow, + } } } \ No newline at end of file