添加DataService为数据服务类,负责数据的加载工作,通过消息来通知DataService更新数据
This commit is contained in:
@@ -19,24 +19,27 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
private readonly DeviceRepository _deviceRepository;
|
||||
private readonly ILogger<DevicesViewModel> _logger;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
[ObservableProperty] private ObservableCollection<Device> _devices;
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
public DevicesViewModel(
|
||||
ILogger<DevicesViewModel> logger, IDialogService dialogService
|
||||
ILogger<DevicesViewModel> logger, IDialogService dialogService, DataServices dataServices
|
||||
)
|
||||
{
|
||||
_deviceRepository = new DeviceRepository();
|
||||
_menuRepository = new MenuRepository();
|
||||
_logger = logger;
|
||||
_dialogService = dialogService;
|
||||
_dataServices = dataServices;
|
||||
|
||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Devices));
|
||||
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
|
||||
}
|
||||
|
||||
public async Task OnLoadedAsync()
|
||||
{
|
||||
var ds = await _deviceRepository.GetAll();
|
||||
Devices = new ObservableCollection<Device>(ds);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
@@ -56,6 +59,7 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
MenuBean deviceMenu = new MenuBean()
|
||||
{
|
||||
Name = device.Name,
|
||||
Type = MenuType.DeviceMenu,
|
||||
Icon = SegoeFluentIcons.Devices4.Glyph,
|
||||
};
|
||||
bool addMenuRes = await _menuRepository.AddDeviceMenu(deviceMenu);
|
||||
|
||||
@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
@@ -13,6 +14,7 @@ namespace PMSWPF.ViewModels;
|
||||
public partial class MainViewModel : ViewModelBase
|
||||
{
|
||||
private readonly NavgatorServices _navgatorServices;
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
[ObservableProperty] private ViewModelBase currentViewModel;
|
||||
[ObservableProperty]
|
||||
@@ -20,32 +22,26 @@ public partial class MainViewModel : ViewModelBase
|
||||
|
||||
private readonly MenuRepository _menuRepository;
|
||||
|
||||
public MainViewModel(NavgatorServices navgatorServices)
|
||||
public MainViewModel(NavgatorServices navgatorServices,DataServices dataServices)
|
||||
{
|
||||
_navgatorServices = navgatorServices;
|
||||
_menuRepository = new MenuRepository();
|
||||
_dataServices = dataServices;
|
||||
|
||||
_navgatorServices.OnViewModelChanged += () => { CurrentViewModel = _navgatorServices.CurrentViewModel; };
|
||||
CurrentViewModel = new HomeViewModel();
|
||||
CurrentViewModel.OnLoaded();
|
||||
|
||||
WeakReferenceMessenger.Default.Register<UpdateMenuMessage>( this,UpdateMenu);
|
||||
WeakReferenceMessenger.Default.Send<LoadMessage>(new LoadMessage(LoadTypes.Menu));
|
||||
dataServices.OnMenuListChanged += (menus) =>
|
||||
{
|
||||
Menus = new ObservableCollection<MenuBean>(menus);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override void OnLoaded()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private async void UpdateMenu(object recipient, UpdateMenuMessage message)
|
||||
{
|
||||
await LoadMenu();
|
||||
}
|
||||
|
||||
|
||||
public override async void OnLoaded()
|
||||
{
|
||||
await LoadMenu();
|
||||
}
|
||||
|
||||
private async Task LoadMenu()
|
||||
{
|
||||
var menuList= await _menuRepository.GetMenu();
|
||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@ namespace PMSWPF.ViewModels;
|
||||
|
||||
public abstract class ViewModelBase : ObservableObject
|
||||
{
|
||||
public Object NavgateParameters { get; set; }
|
||||
public abstract void OnLoaded();
|
||||
}
|
||||
Reference in New Issue
Block a user