From fd68a223fdc69f2aa1098f33eae99c4e107d4283 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 13 Jun 2025 18:54:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BE=E5=A4=87=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E7=AE=80=E5=8D=95=E5=B8=83=E5=B1=80=EF=BC=8C?= =?UTF-8?q?=E5=BC=95=E5=85=A5GridView=E7=9A=84=E4=BD=BF=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E6=8E=A5GridView=E7=9A=84=E6=A8=A1=E6=9D=BF=E6=8A=BD?= =?UTF-8?q?=E7=A6=BB=E6=88=90Dictionary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml | 3 ++ Extensions/TaskExtensions.cs | 19 ++++++++++ PMSWPF.csproj | 3 -- Resources/DevicesItemTemplateDictionary.xaml | 16 +++++++++ Services/DeviceDialogService.cs | 5 +++ Services/IDeviceDialogService.cs | 2 ++ Services/NavgatorServices.cs | 1 + ViewModels/DataTransformViewModel.cs | 5 ++- ViewModels/DevicesViewModel.cs | 37 ++++++++++++++++++-- ViewModels/HomeViewModel.cs | 5 ++- ViewModels/ViewModelBase.cs | 4 ++- Views/DevicesView.xaml | 12 ++++++- Views/DevicesView.xaml.cs | 16 ++++++++- 13 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 Extensions/TaskExtensions.cs create mode 100644 Resources/DevicesItemTemplateDictionary.xaml diff --git a/App.xaml b/App.xaml index c02ef92..a51b561 100644 --- a/App.xaml +++ b/App.xaml @@ -10,7 +10,10 @@ + + + diff --git a/Extensions/TaskExtensions.cs b/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..1886586 --- /dev/null +++ b/Extensions/TaskExtensions.cs @@ -0,0 +1,19 @@ +namespace PMSWPF.Extensions; + +public static class TaskExtensions +{ + + public static async Task Await(this Task task,Action onError=null,Action onComplete=null) + { + try + { + await task; + onComplete?.Invoke(); + } + catch (Exception e) + { + onError?.Invoke(e); + } + } + +} \ No newline at end of file diff --git a/PMSWPF.csproj b/PMSWPF.csproj index 4fef5c5..e208e8a 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -17,8 +17,5 @@ - - - diff --git a/Resources/DevicesItemTemplateDictionary.xaml b/Resources/DevicesItemTemplateDictionary.xaml new file mode 100644 index 0000000..657234f --- /dev/null +++ b/Resources/DevicesItemTemplateDictionary.xaml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Services/DeviceDialogService.cs b/Services/DeviceDialogService.cs index e22eac1..25a66d0 100644 --- a/Services/DeviceDialogService.cs +++ b/Services/DeviceDialogService.cs @@ -27,4 +27,9 @@ public class DeviceDialogService : IDeviceDialogService } } + + public void ShowMessageDialog(string title, string message) + { + MessageBox.Show(message); + } } \ No newline at end of file diff --git a/Services/IDeviceDialogService.cs b/Services/IDeviceDialogService.cs index ad5a1a0..2b58c35 100644 --- a/Services/IDeviceDialogService.cs +++ b/Services/IDeviceDialogService.cs @@ -5,4 +5,6 @@ namespace PMSWPF.Services; public interface IDeviceDialogService { Task ShowAddDeviceDialog(); + + void ShowMessageDialog(string title, string message); } \ No newline at end of file diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index b0b96fb..3385f01 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -16,6 +16,7 @@ public class NavgatorServices { currentViewModel = value; OnViewModelChanged?.Invoke(); + currentViewModel.OnLoaded(); } } diff --git a/ViewModels/DataTransformViewModel.cs b/ViewModels/DataTransformViewModel.cs index 7842222..f75088e 100644 --- a/ViewModels/DataTransformViewModel.cs +++ b/ViewModels/DataTransformViewModel.cs @@ -2,5 +2,8 @@ public class DataTransformViewModel:ViewModelBase { - + public override void OnLoaded() + { + + } } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 1276ec8..4c92518 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System.Collections.ObjectModel; +using System.Windows; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; @@ -16,13 +18,27 @@ public partial class DevicesViewModel : ViewModelBase { private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; - + [ObservableProperty] + private ObservableCollection _devices; public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories) { _deviceDialogService = deviceDialogService; _devicesRepositories = devicesRepositories; } + public async Task OnLoadedAsync() + { + var ds = await _devicesRepositories.GetAll(); + _devices = new ObservableCollection(); + + foreach (var dbDevice in ds) + { + Device device = new Device(); + dbDevice.CopyTo(device); + _devices.Add(device); + } + } + [RelayCommand] public async void AddDevice() @@ -35,7 +51,11 @@ public partial class DevicesViewModel : ViewModelBase { DbDevice dbDevice = new DbDevice(); device.CopyTo(dbDevice); - await _devicesRepositories.Add(dbDevice); + var rowCount= await _devicesRepositories.Add(dbDevice); + if (rowCount>0) + { + MessageBox.Show("Device added successfully"); + } } } @@ -51,4 +71,15 @@ public partial class DevicesViewModel : ViewModelBase MessageBox.Show(e.Message); } } + + public override void OnLoaded() + { + OnLoadedAsync().Await((e) => + { + _deviceDialogService.ShowMessageDialog("",e.Message); + }, () => + { + + }); + } } \ No newline at end of file diff --git a/ViewModels/HomeViewModel.cs b/ViewModels/HomeViewModel.cs index 2209d82..59662f4 100644 --- a/ViewModels/HomeViewModel.cs +++ b/ViewModels/HomeViewModel.cs @@ -2,5 +2,8 @@ public class HomeViewModel:ViewModelBase { - + public override void OnLoaded() + { + + } } \ No newline at end of file diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index 682ecdf..a3020fd 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -2,10 +2,12 @@ namespace PMSWPF.ViewModels; -public partial class ViewModelBase:ObservableObject +public abstract partial class ViewModelBase:ObservableObject { public ViewModelBase() { } + + public abstract void OnLoaded(); } \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 75155d4..2517eae 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -3,8 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" + xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" xmlns:local="clr-namespace:PMSWPF.Views" xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs" + xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:PMSWPF.ViewModels" d:DataContext="{d:DesignInstance vm:DevicesViewModel}" mc:Ignorable="d" @@ -13,7 +16,14 @@