完成显示主界面

This commit is contained in:
2025-07-26 12:19:05 +08:00
parent 2ee17c2560
commit 86dc3b670a
9 changed files with 18 additions and 36 deletions

BIN
DMS.WPF/Assets/AppIcon2.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<Resource Include="Assets\AppIcon.png" /> <Resource Include="Assets\AppIcon.png" />
<Resource Include="Assets\AppIcon2.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -15,8 +15,4 @@ public interface INavigationService
/// <param name="parameter">要传递给目标ViewModel的参数。</param> /// <param name="parameter">要传递给目标ViewModel的参数。</param>
Task NavigateToAsync(string viewKey, object parameter = null); Task NavigateToAsync(string viewKey, object parameter = null);
/// <summary>
/// 显示主窗口。
/// </summary>
Task ShowMainWindowAsync();
} }

View File

@@ -48,20 +48,7 @@ public class NavigationService : INavigationService
mainViewModel.CurrentViewModel = viewModel; mainViewModel.CurrentViewModel = viewModel;
} }
/// <summary>
/// 显示主窗口。
/// </summary>
public Task ShowMainWindowAsync()
{
return App.Current.Dispatcher.InvokeAsync(() =>
{
var mainView = _serviceProvider.GetRequiredService<MainView>();
// 将 MainView 设置为新的主窗口
App.Current.MainWindow = mainView;
mainView.Show();
})
.Task;
}
private Type GetViewModelTypeByKey(string key) private Type GetViewModelTypeByKey(string key)
{ {

View File

@@ -28,7 +28,6 @@ public partial class MainViewModel : ViewModelBase
private readonly DataServices _dataServices; private readonly DataServices _dataServices;
private readonly IDialogService _dialogService; private readonly IDialogService _dialogService;
private readonly ILogger<MainViewModel> _logger; private readonly ILogger<MainViewModel> _logger;
private readonly NavgatorServices _navgatorServices;
/// <summary> /// <summary>
/// 当前显示的视图模型。 /// 当前显示的视图模型。
@@ -49,22 +48,18 @@ public partial class MainViewModel : ViewModelBase
/// <param name="dataServices">数据服务。</param> /// <param name="dataServices">数据服务。</param>
/// <param name="dialogService">对话框服务。</param> /// <param name="dialogService">对话框服务。</param>
/// <param name="logger">日志记录器。</param> /// <param name="logger">日志记录器。</param>
public MainViewModel(NavgatorServices navgatorServices, DataServices dataServices, IDialogService dialogService, public MainViewModel(
ILogger<MainViewModel> logger) ILogger<MainViewModel> logger)
{ {
_navgatorServices = navgatorServices; // _dataServices = dataServices;
_dataServices = dataServices;
_dialogService = dialogService;
_logger = logger; _logger = logger;
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
CurrentViewModel = new HomeViewModel(); CurrentViewModel = new HomeViewModel();
CurrentViewModel.OnLoaded(); CurrentViewModel.OnLoaded();
// 发送消息加载数据 // 发送消息加载数据
MessageHelper.SendLoadMessage(LoadTypes.All); MessageHelper.SendLoadMessage(LoadTypes.All);
// 当菜单加载成功后,在前台显示菜单 // 当菜单加载成功后,在前台显示菜单
dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection<MenuBean>(menus); }; // dataServices.OnMenuTreeListChanged += (menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
} }
/// <summary> /// <summary>

View File

@@ -6,6 +6,8 @@ using DMS.WPF.Services;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using DMS.Application.Services; using DMS.Application.Services;
using DMS.WPF.Views;
using Microsoft.Extensions.DependencyInjection;
namespace DMS.WPF.ViewModels; namespace DMS.WPF.ViewModels;
@@ -38,24 +40,27 @@ public partial class SplashViewModel : ObservableObject
LoadingMessage = "正在加载系统配置..."; LoadingMessage = "正在加载系统配置...";
// 可以在这里添加加载配置的逻辑 // 可以在这里添加加载配置的逻辑
await Task.Delay(1500); // 模拟耗时 await Task.Delay(500); // 模拟耗时
LoadingMessage = "正在连接后台服务..."; LoadingMessage = "正在连接后台服务...";
// 可以在这里添加连接服务的逻辑 // 可以在这里添加连接服务的逻辑
await Task.Delay(1500); // 模拟耗时 await Task.Delay(500); // 模拟耗时
LoadingMessage = "加载完成,正在启动主界面..."; LoadingMessage = "加载完成,正在启动主界面...";
await Task.Delay(1500); await Task.Delay(500);
// 初始化完成,显示主窗口 // 初始化完成,显示主窗口
var navigationService = (INavigationService)_serviceProvider.GetService(typeof(INavigationService)); var mainView = App.Current.Services.GetRequiredService<MainView>();
await navigationService.ShowMainWindowAsync(); // 将 MainView 设置为新的主窗口
App.Current.MainWindow = mainView;
mainView.Show();
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
// 处理初始化过程中的异常 // 处理初始化过程中的异常
LoadingMessage = $"初始化失败: {ex.Message}"; LoadingMessage = $"初始化失败: {ex.Message}";
Console.WriteLine($"初始化失败: {ex.Message}");
// 在此可以记录日志或显示错误对话框 // 在此可以记录日志或显示错误对话框
return false; return false;
} }

View File

@@ -42,7 +42,7 @@
<Grid> <Grid>
<taskbarNotification:TaskbarIcon x:Name="MyNotifyIcon" <taskbarNotification:TaskbarIcon x:Name="MyNotifyIcon"
ToolTipText="设备管理系统" ToolTipText="设备管理系统"
IconSource="/AppIcon2.ico" IconSource="/Assets/AppIcon2.ico"
DoubleClickCommand="{Binding ShowWindowCommand}"> DoubleClickCommand="{Binding ShowWindowCommand}">
<taskbarNotification:TaskbarIcon.ContextMenu> <taskbarNotification:TaskbarIcon.ContextMenu>
<ContextMenu> <ContextMenu>

View File

@@ -15,14 +15,12 @@ namespace DMS.WPF.Views;
public partial class MainView : Window public partial class MainView : Window
{ {
private readonly DataServices _dataServices;
private MainViewModel _viewModel; private MainViewModel _viewModel;
public MainView(DataServices dataServices) public MainView()
{ {
InitializeComponent(); InitializeComponent();
_viewModel = App.Current.Services.GetRequiredService<MainViewModel>(); _viewModel = App.Current.Services.GetRequiredService<MainViewModel>();
_dataServices = dataServices;
DataContext = _viewModel; DataContext = _viewModel;
NlogHelper.Info("主界面加载成功"); NlogHelper.Info("主界面加载成功");

View File

@@ -10,7 +10,7 @@
<Grid> <Grid>
<Border CornerRadius="10" Background="#FF333333"> <Border CornerRadius="10" Background="#FF333333">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Image Source="/Assets/AppIcon.png" Width="100" Height="100"/> <Image Source="/Assets/AppIcon2.ico" Width="100" Height="100"/>
<TextBlock Text="{Binding LoadingMessage}" Foreground="White" FontSize="16" Margin="0,20,0,0"/> <TextBlock Text="{Binding LoadingMessage}" Foreground="White" FontSize="16" Margin="0,20,0,0"/>
</StackPanel> </StackPanel>
</Border> </Border>