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 a097c17c2..0718ad590 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs +++ b/System.Application.SteamTools.Client.Desktop.Avalonia.App/App.axaml.cs @@ -215,6 +215,12 @@ namespace System.Application.UI public Window? MainWindow { get; set; } + public void ShowChildWindow(object window) + { + if (window is Window w) + w.ShowDialog(MainWindow); + } + /// /// Restores the app's main window by setting its WindowState to /// WindowState.Normal and showing the window. diff --git a/System.Application.SteamTools.Client.Desktop.Avalonia.App/Startup.cs b/System.Application.SteamTools.Client.Desktop.Avalonia.App/Startup.cs index c8ff7ea0c..896c3d050 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia.App/Startup.cs +++ b/System.Application.SteamTools.Client.Desktop.Avalonia.App/Startup.cs @@ -27,6 +27,8 @@ namespace System.Application.UI static void ConfigureServices(IServiceCollection services) { + services.AddSingleton(); + // 桌面平台服务 此项放在其他通用业务实现服务之前 services.AddDesktopPlatformService(); @@ -107,6 +109,7 @@ namespace System.Application.UI * - 按钮文本(ButtonText)缺少本地化翻译(Translate) * - 某些图标图片与枚举值不太匹配,例如 Information */ + services.AddMessageWindowService(); #if WINDOWS // 可选项,在 Win 平台使用 WPF 实现的 MessageBox 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 new file mode 100644 index 000000000..b9c6a4516 --- /dev/null +++ b/System.Application.SteamTools.Client.Desktop.Avalonia/Application/Services/Implementation/MessageWindowServiceImpl.cs @@ -0,0 +1,38 @@ +using Avalonia.Controls; +using Avalonia.Threading; +using System.Application.UI; +using System.Application.UI.ViewModels.Windows; +using System.Application.UI.Windows; +using System.Threading.Tasks; + +namespace System.Application.Services.Implementation +{ + internal sealed class MessageWindowServiceImpl : IMessageWindowService + { + public Task ShowDialog(string messageBoxText) + { + return ShowDialog(messageBoxText, "", false); + } + + public Task ShowDialog(string messageBoxText, string caption) + { + return ShowDialog(messageBoxText, caption, false); + } + + public Task ShowDialog(string messageBoxText, string caption, bool isCancelcBtn) + { + return MainThreadDesktop.InvokeOnMainThreadAsync(() => + { + var dialog = new MessageWindowViewModel + { + Content = messageBoxText, + Title = caption + }; + var window = new MessageWindow { DataContext = dialog }; + window.WindowStartupLocation = WindowStartupLocation.CenterScreen; + AppHelper.Current.ShowChildWindow(window); + return dialog.DialogResult; + }); + } + } +} \ No newline at end of file diff --git a/System.Application.SteamTools.Client.Desktop.Avalonia/Application/UI/Windows/MessageWindow.axaml b/System.Application.SteamTools.Client.Desktop.Avalonia/Application/UI/Windows/MessageWindow.axaml index adac8286b..598578e66 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia/Application/UI/Windows/MessageWindow.axaml +++ b/System.Application.SteamTools.Client.Desktop.Avalonia/Application/UI/Windows/MessageWindow.axaml @@ -4,7 +4,8 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="200" x:Class="System.Application.UI.Windows.MessageWindow" - Title="MessageWindow"> - - + Title="{Binding Title}"> + + + diff --git a/System.Application.SteamTools.Client.Desktop.Avalonia/ServiceCollectionExtensions.cs b/System.Application.SteamTools.Client.Desktop.Avalonia/ServiceCollectionExtensions.cs index 5ef3630d5..7a1b67269 100644 --- a/System.Application.SteamTools.Client.Desktop.Avalonia/ServiceCollectionExtensions.cs +++ b/System.Application.SteamTools.Client.Desktop.Avalonia/ServiceCollectionExtensions.cs @@ -19,6 +19,17 @@ namespace Microsoft.Extensions.DependencyInjection return services; } + /// + /// 添加弹窗服务 + /// + /// + /// + public static IServiceCollection AddMessageWindowService(this IServiceCollection services) + { + services.AddSingleton(); + return services; + } + /// /// 添加应用程序更新服务 /// diff --git a/System.Application.SteamTools.Client.Desktop/Services/IDesktopAppService.cs b/System.Application.SteamTools.Client.Desktop/Services/IDesktopAppService.cs index 11b812c74..b2e55c5bd 100644 --- a/System.Application.SteamTools.Client.Desktop/Services/IDesktopAppService.cs +++ b/System.Application.SteamTools.Client.Desktop/Services/IDesktopAppService.cs @@ -11,6 +11,11 @@ namespace System.Application.Services /// AppTheme Theme { get; set; } + /// + /// 打开子窗口 + /// + void ShowChildWindow(object window); + /// /// 退出整个程序 /// diff --git a/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs b/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs new file mode 100644 index 000000000..333178935 --- /dev/null +++ b/System.Application.SteamTools.Client.Desktop/Services/IMessageWindowService.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using System.Windows; + +namespace System.Application.Services +{ + public interface IMessageWindowService + { + /// + /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮,并且返回结果。 + /// + /// 一个 ,用于指定要显示的文本。 + /// 一个 值,用于指定用户单击了哪个消息框按钮。 + Task ShowDialog(string messageBoxText); + + /// + /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮,并且返回结果。 + /// + /// 一个 ,用于指定要显示的文本。 + /// 一个 ,用于指定要显示的标题栏标题。 + /// 一个 值,用于指定用户单击了哪个消息框按钮。 + Task ShowDialog(string messageBoxText, string caption); + + /// + /// 显示一个消息框,该消息框包含消息、标题栏标题、按钮,并且返回结果。 + /// + /// 一个 ,用于指定要显示的文本。 + /// 一个 ,用于指定要显示的标题栏标题。 + /// 一个 值,用于指定要显示哪个按钮或哪些按钮。 + /// 一个 值,用于指定用户单击了哪个消息框按钮。 + Task ShowDialog(string messageBoxText, string caption, bool isCancelcBtn); + } +} \ No newline at end of file diff --git a/System.Application.SteamTools.Client.Desktop/UI/Resx/R.cs b/System.Application.SteamTools.Client.Desktop/UI/Resx/R.cs index 111a5d5d1..7722ece55 100644 --- a/System.Application.SteamTools.Client.Desktop/UI/Resx/R.cs +++ b/System.Application.SteamTools.Client.Desktop/UI/Resx/R.cs @@ -71,7 +71,7 @@ namespace System.Application.UI.Resx /// public static void ChangeLanguage(string cultureName) { - if (IsMatch(CultureInfo.CurrentUICulture, cultureName)) return; + if (IsMatch(AppResources.Culture, cultureName)) return; if (string.IsNullOrWhiteSpace(cultureName)) AppResources.Culture = DefaultCurrentUICulture; else diff --git a/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Base/WindowViewModel.cs b/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Base/WindowViewModel.cs index 87e1f9f4b..fd7c99b93 100644 --- a/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Base/WindowViewModel.cs +++ b/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Base/WindowViewModel.cs @@ -13,6 +13,7 @@ namespace System.Application.UI.ViewModels public bool IsInitialized { get; set; } + public bool DialogResult { get; protected set; } protected void InvokeOnUIDispatcher(Action action) { MainThreadDesktop.BeginInvokeOnMainThread(action); diff --git a/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Pages/DebugPageViewModel.cs b/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Pages/DebugPageViewModel.cs index 5d843f1a0..635dc936d 100644 --- a/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Pages/DebugPageViewModel.cs +++ b/System.Application.SteamTools.Client.Desktop/UI/ViewModels/Pages/DebugPageViewModel.cs @@ -1,4 +1,5 @@ using ReactiveUI; +using System.Application.Services; using System.Application.UI.Resx; using System.Collections.Generic; using System.Linq; @@ -30,6 +31,8 @@ namespace System.Application.UI.ViewModels Services.ToastService.Current.Notify("Test CommandTest CommandTest CommandTest CommandTest CommandTest Command"); DebugString += Services.ToastService.Current.Message + Environment.NewLine; DebugString += Services.ToastService.Current.IsVisible + Environment.NewLine; + + DI.Get().ShowDialog("Test", "Test"); } } } \ No newline at end of file