using System.Threading.Tasks; using System.Threading; using AIK.Service.Extensions; using AIK.Service.IService; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AIK.Service.Service { public class WindowService : IWindowService { private readonly IServiceProvider _serviceProvider; private readonly IWindowFactory _windowFactory; private readonly Dictionary _windowViewModelMappings; private readonly Dictionary _windowKeyMappings; private class WindowMapping { public Type WindowType { get; set; } public Type ViewModelType { get; set; } } public WindowService(IServiceProvider serviceProvider, IWindowFactory windowFactory) { _serviceProvider = serviceProvider; _windowFactory = windowFactory; _windowViewModelMappings = new Dictionary(); _windowKeyMappings = new Dictionary(); } public void RegisterWindow() where TWindow : Window where TViewModel : class { _windowViewModelMappings[typeof(TViewModel)] = typeof(TWindow); } // 基于字符串键的实现 public void Show(string windowKey) { if (_windowKeyMappings.TryGetValue(windowKey, out var mapping)) { var window = _windowFactory.CreateWindow(mapping.WindowType); window.Show(); } else { throw new ArgumentException($"未找到键为 '{windowKey}' 的窗口映射"); } } public void Show(string windowKey, TViewModel viewModel) where TViewModel : class { if (_windowKeyMappings.TryGetValue(windowKey, out var mapping)) { // 使用非泛型方法替代反射 var window = _windowFactory.CreateWindow(mapping.WindowType, typeof(TViewModel), viewModel); window?.Show(); } else { throw new ArgumentException($"未找到键为 '{windowKey}' 的窗口映射"); } } public void Show(string windowKey, TViewModel viewModel, Action initializeAction) where TViewModel : class { if (_windowKeyMappings.TryGetValue(windowKey, out var mapping)) { // 使用非泛型方法替代反射 var window = _windowFactory.CreateWindow(mapping.WindowType, typeof(TViewModel), viewModel); initializeAction?.Invoke(window, viewModel); window?.Show(); } else { throw new ArgumentException($"未找到键为 '{windowKey}' 的窗口映射"); } } public bool? ShowDialog(string windowKey) { if (_windowKeyMappings.TryGetValue(windowKey, out var mapping)) { var window = _windowFactory.CreateWindow(mapping.WindowType); return window.ShowDialog(); } else { throw new ArgumentException($"未找到键为 '{windowKey}' 的窗口映射"); } } public bool? ShowDialog(string windowKey, TViewModel viewModel) where TViewModel : class { if (_windowKeyMappings.TryGetValue(windowKey, out var mapping)) { var method = typeof(IWindowFactory).GetMethod(nameof(IWindowFactory.CreateWindow)) ?.MakeGenericMethod(mapping.WindowType, typeof(TViewModel)); var window = method?.Invoke(_windowFactory, new object[] { viewModel }) as Window; return window?.ShowDialog(); } else { throw new ArgumentException($"未找到键为 '{windowKey}' 的窗口映射"); } } public void RegisterWindow(string windowKey, Type windowType, Type viewModelType = null) { _windowKeyMappings[windowKey] = new WindowMapping { WindowType = windowType, ViewModelType = viewModelType }; } } }