添加消息通知功能,使用Handy Control的Grow来实现的
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using HandyControl.Controls;
|
||||
using HandyControl.Data;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Excptions;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Helper;
|
||||
@@ -11,6 +13,8 @@ using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
using PMSWPF.Views.Dialogs;
|
||||
using MessageBox = System.Windows.MessageBox;
|
||||
using Notification = PMSWPF.Models.Notification;
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
@@ -18,59 +22,74 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDeviceDialogService _deviceDialogService;
|
||||
private readonly DevicesRepositories _devicesRepositories;
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<Device> _devices;
|
||||
public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories)
|
||||
private readonly INotificationService _notificationService;
|
||||
[ObservableProperty] private ObservableCollection<Device> _devices = new ();
|
||||
|
||||
public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,INotificationService notificationService)
|
||||
{
|
||||
_deviceDialogService = deviceDialogService;
|
||||
_devicesRepositories = devicesRepositories;
|
||||
_devices = new ObservableCollection<Device>();
|
||||
_notificationService = notificationService;
|
||||
}
|
||||
|
||||
public async Task OnLoadedAsync()
|
||||
{
|
||||
var ds = await _devicesRepositories.GetAll();
|
||||
|
||||
_devices.Clear();
|
||||
foreach (var dbDevice in ds)
|
||||
{
|
||||
var deviceExist= _devices.FirstOrDefault(d => d.Id == dbDevice.Id);
|
||||
if (deviceExist == null)
|
||||
{
|
||||
Device device = new Device();
|
||||
dbDevice.CopyTo(device);
|
||||
_devices.Add(device);
|
||||
}
|
||||
|
||||
Device device = new Device();
|
||||
dbDevice.CopyTo(device);
|
||||
_devices.Add(device);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public void Test()
|
||||
{
|
||||
// GrowlInfo info = new GrowlInfo();
|
||||
// info.Message = "Hello";
|
||||
// info.Type = InfoType.Error;
|
||||
// info.ShowCloseButton = true;
|
||||
_notificationService.Show( "Hello",NotificationType.Info,true);
|
||||
// Growl.Error("Hello");
|
||||
// Growl.Info("Hello");
|
||||
// Growl.Success("Hello");
|
||||
// Growl.WarningGlobal("Hello");
|
||||
// Growl.SuccessGlobal("Hello");
|
||||
// Growl.FatalGlobal("Hello");
|
||||
// Growl.Ask("Hello", isConfirmed =>
|
||||
// {
|
||||
// Growl.Info(isConfirmed.ToString());
|
||||
// return true;
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async void AddDevice()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Device device= await _deviceDialogService.ShowAddDeviceDialog();
|
||||
if (device != null)
|
||||
{
|
||||
DbDevice dbDevice = new DbDevice();
|
||||
device.CopyTo<DbDevice>(dbDevice);
|
||||
var rowCount= await _devicesRepositories.Add(dbDevice);
|
||||
if (rowCount>0)
|
||||
{
|
||||
// MessageBox.Show("Device added successfully");
|
||||
await OnLoadedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Device device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||
if (device != null)
|
||||
{
|
||||
DbDevice dbDevice = new DbDevice();
|
||||
device.CopyTo<DbDevice>(dbDevice);
|
||||
var rowCount = await _devicesRepositories.Add(dbDevice);
|
||||
if (rowCount > 0)
|
||||
{
|
||||
// MessageBox.Show("Device added successfully");
|
||||
await OnLoadedAsync();
|
||||
_notificationService.Show(new Notification(){Message = "Hello World!",Type = NotificationType.Success,IsGlobal = true});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (DbExistException e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
MessageBox.Show(e.Message);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -81,12 +100,6 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
|
||||
public override void OnLoaded()
|
||||
{
|
||||
OnLoadedAsync().Await((e) =>
|
||||
{
|
||||
_deviceDialogService.ShowMessageDialog("",e.Message);
|
||||
}, () =>
|
||||
{
|
||||
|
||||
});
|
||||
OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user