添加启动页
This commit is contained in:
14
DMS.WPF/Services/INavigatable.cs
Normal file
14
DMS.WPF/Services/INavigatable.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
// 文件: DMS.WPF/Services/INavigatable.cs
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 定义了一个契约,表示ViewModel可以安全地接收导航传入的参数。
|
||||
/// </summary>
|
||||
public interface INavigatable
|
||||
{
|
||||
/// <summary>
|
||||
/// 当导航到此ViewModel时,由导航服务调用此方法,以传递参数。
|
||||
/// </summary>
|
||||
/// <param name="parameter">从导航源传递过来的参数对象。</param>
|
||||
Task OnNavigatedToAsync(object parameter);
|
||||
}
|
||||
22
DMS.WPF/Services/INavigationService.cs
Normal file
22
DMS.WPF/Services/INavigationService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
// 文件: DMS.WPF/Services/INavigationService.cs
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 定义了应用程序的导航服务接口。
|
||||
/// </summary>
|
||||
public interface INavigationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 导航到由唯一键标识的视图,并传递一个参数。
|
||||
/// </summary>
|
||||
/// <param name="viewKey">在DI容器中注册的目标视图的唯一键(通常是ViewModel的名称)。</param>
|
||||
/// <param name="parameter">要传递给目标ViewModel的参数。</param>
|
||||
Task NavigateToAsync(string viewKey, object parameter = null);
|
||||
|
||||
/// <summary>
|
||||
/// 显示主窗口。
|
||||
/// </summary>
|
||||
Task ShowMainWindowAsync();
|
||||
}
|
||||
72
DMS.WPF/Services/NavigationService.cs
Normal file
72
DMS.WPF/Services/NavigationService.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
// 文件: DMS.WPF/Services/NavigationService.cs
|
||||
using DMS.WPF.ViewModels;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.ViewModels;
|
||||
|
||||
namespace DMS.WPF.Services;
|
||||
|
||||
/// <summary>
|
||||
/// INavigationService 的实现,负责解析ViewModel并处理参数传递。
|
||||
/// </summary>
|
||||
public class NavigationService : INavigationService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数。
|
||||
/// </summary>
|
||||
public NavigationService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导航到指定键的视图,并传递参数。
|
||||
/// </summary>
|
||||
public async Task NavigateToAsync(string viewKey, object parameter = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(viewKey))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var mainViewModel = _serviceProvider.GetRequiredService<MainViewModel>();
|
||||
var viewModelType = GetViewModelTypeByKey(viewKey);
|
||||
var viewModel = _serviceProvider.GetRequiredService(viewModelType) as ViewModelBase;
|
||||
|
||||
if (viewModel is INavigatable navigatableViewModel)
|
||||
{
|
||||
await navigatableViewModel.OnNavigatedToAsync(parameter);
|
||||
}
|
||||
|
||||
mainViewModel.CurrentViewModel = viewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示主窗口。
|
||||
/// </summary>
|
||||
public Task ShowMainWindowAsync()
|
||||
{
|
||||
var mainWindow = _serviceProvider.GetRequiredService<MainWindow>();
|
||||
mainWindow.Show();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Type GetViewModelTypeByKey(string key)
|
||||
{
|
||||
return key switch
|
||||
{
|
||||
"HomeView" => typeof(HomeViewModel),
|
||||
"DevicesView" => typeof(DevicesViewModel),
|
||||
"DeviceDetailView" => typeof(DeviceDetailViewModel),
|
||||
"VariableTableView" => typeof(VariableTableViewModel),
|
||||
"MqttsView" => typeof(MqttsViewModel),
|
||||
"MqttServerDetailView" => typeof(MqttServerDetailViewModel),
|
||||
"SettingView" => typeof(SettingViewModel),
|
||||
_ => throw new KeyNotFoundException($"未找到与键 '{key}' 关联的视图模型类型。请检查 NavigationService 的映射配置。")
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user