2025-06-23 17:01:06 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
using PMSWPF.Message;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
using PMSWPF.ViewModels;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Services;
|
|
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public class NavgatorServices : ObservableRecipient, IRecipient<NavgatorMessage>
|
2025-06-10 20:55:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
private ViewModelBase currentViewModel;
|
|
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public NavgatorServices()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsActive = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-10 20:55:39 +08:00
|
|
|
|
public ViewModelBase CurrentViewModel
|
|
|
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
|
get => currentViewModel;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
set
|
|
|
|
|
|
{
|
2025-06-23 17:01:06 +08:00
|
|
|
|
currentViewModel = value;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
OnViewModelChanged?.Invoke();
|
2025-06-13 18:54:17 +08:00
|
|
|
|
currentViewModel.OnLoaded();
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public void Receive(NavgatorMessage message)
|
|
|
|
|
|
{
|
|
|
|
|
|
CurrentViewModel = message.Value;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
CurrentViewModel.NavgateParameters = message.Parameters;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public event Action OnViewModelChanged;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
|
|
|
|
|
|
public void NavigateTo<T>() where T : ViewModelBase
|
|
|
|
|
|
{
|
|
|
|
|
|
// Application.Current
|
|
|
|
|
|
CurrentViewModel = App.Current.Services.GetService<T>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|