兼容win7版应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.4 KiB

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<Type, Type> _windowViewModelMappings;
private readonly Dictionary<string, WindowMapping> _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<Type, Type>();
_windowKeyMappings = new Dictionary<string, WindowMapping>();
}
public void RegisterWindow<TWindow, TViewModel>()
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<TViewModel>(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<TViewModel>(string windowKey, TViewModel viewModel, Action<Window, object> 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<TViewModel>(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
};
}
}
}