From ea15ea594ab673a2b7226ab2c238c76006a9997a Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 12 Jun 2025 13:15:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=9A=84=E5=BC=B9=E5=87=BA=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E6=A1=86,=E6=9C=AA=E5=AE=8C=E6=88=90=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E6=A1=86=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 1 + Data/Repositories/DevicesRepositories.cs | 14 +++--- Services/DeviceDialogService.cs | 20 +++++++++ Services/IDeviceDialogService.cs | 8 ++++ ViewModels/DevicesViewModel.cs | 27 ++++++++--- ViewModels/Dialogs/DeviceDialogViewModel.cs | 13 ++++++ ViewModels/ViewModelBase.cs | 11 +++-- Views/DevicesView.xaml | 8 +++- Views/Dialogs/DeviceDialog.xaml | 50 +++++++++++++++++++++ Views/Dialogs/DeviceDialog.xaml.cs | 48 ++++++++++++++++++++ 10 files changed, 181 insertions(+), 19 deletions(-) create mode 100644 Services/DeviceDialogService.cs create mode 100644 Services/IDeviceDialogService.cs create mode 100644 ViewModels/Dialogs/DeviceDialogViewModel.cs create mode 100644 Views/Dialogs/DeviceDialog.xaml create mode 100644 Views/Dialogs/DeviceDialog.xaml.cs diff --git a/App.xaml.cs b/App.xaml.cs index d1ef0f7..a61ba8a 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -21,6 +21,7 @@ namespace PMSWPF { var container = new ServiceCollection(); container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); diff --git a/Data/Repositories/DevicesRepositories.cs b/Data/Repositories/DevicesRepositories.cs index 33fecca..1c81b13 100644 --- a/Data/Repositories/DevicesRepositories.cs +++ b/Data/Repositories/DevicesRepositories.cs @@ -9,21 +9,21 @@ public class DevicesRepositories:BaseRepositories } - public async Task Add(DbPLC dbPLC) + public async Task Add(DbDevice dbDevice) { - return await _db.Insertable(dbPLC).ExecuteCommandAsync(); + return await _db.Insertable(dbDevice).ExecuteCommandAsync(); } - public async Task> GetAll() + public async Task> GetAll() { - return await _db.Queryable().ToListAsync(); + return await _db.Queryable().ToListAsync(); } - public async Task GetById(int id) + public async Task GetById(int id) { - return await _db.Queryable().FirstAsync(p=>p.id == id); + return await _db.Queryable().FirstAsync(p=>p.Id == id); } public async Task DeleteById(int id) { - return await _db.Deleteable(new DbPLC() { id = id }).ExecuteCommandAsync(); + return await _db.Deleteable(new DbDevice() { Id = id }).ExecuteCommandAsync(); } } \ No newline at end of file diff --git a/Services/DeviceDialogService.cs b/Services/DeviceDialogService.cs new file mode 100644 index 0000000..570ba4d --- /dev/null +++ b/Services/DeviceDialogService.cs @@ -0,0 +1,20 @@ +using PMSWPF.Models; +using PMSWPF.ViewModels.Dialogs; +using PMSWPF.Views.Dialogs; + +namespace PMSWPF.Services; + +public class DeviceDialogService : IDeviceDialogService +{ + public async Task ShowAddDeviceDialog(Device device) + { + DeviceDialogViewModel ddvm = new DeviceDialogViewModel() + { + Title = "添加设备" + }; + + DeviceDialog dialog = new DeviceDialog(ddvm); + await dialog.ShowAsync(); + return device; + } +} \ No newline at end of file diff --git a/Services/IDeviceDialogService.cs b/Services/IDeviceDialogService.cs new file mode 100644 index 0000000..61a66b8 --- /dev/null +++ b/Services/IDeviceDialogService.cs @@ -0,0 +1,8 @@ +using PMSWPF.Models; + +namespace PMSWPF.Services; + +public interface IDeviceDialogService +{ + Task ShowAddDeviceDialog(Device device); +} \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index d8d3171..4c3899c 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,12 +1,25 @@ -namespace PMSWPF.ViewModels; +using CommunityToolkit.Mvvm.Input; +using PMSWPF.Models; +using PMSWPF.Services; +using PMSWPF.ViewModels.Dialogs; +using PMSWPF.Views.Dialogs; -public class DevicesViewModel:ViewModelBase +namespace PMSWPF.ViewModels; + +public partial class DevicesViewModel : ViewModelBase { - public DevicesViewModel() + private readonly IDeviceDialogService _deviceDialogService; + + public DevicesViewModel(IDeviceDialogService deviceDialogService) { - + _deviceDialogService = deviceDialogService; + } + + + [RelayCommand] + public async void AddDevice() + { + Device device = new Device(); + await _deviceDialogService.ShowAddDeviceDialog(device); } - - - } \ No newline at end of file diff --git a/ViewModels/Dialogs/DeviceDialogViewModel.cs b/ViewModels/Dialogs/DeviceDialogViewModel.cs new file mode 100644 index 0000000..c90f6c5 --- /dev/null +++ b/ViewModels/Dialogs/DeviceDialogViewModel.cs @@ -0,0 +1,13 @@ +using CommunityToolkit.Mvvm.ComponentModel; + +namespace PMSWPF.ViewModels.Dialogs; + +public partial class DeviceDialogViewModel:ObservableObject +{ + [ObservableProperty] + private string title="添加设备"; + public DeviceDialogViewModel() + { + + } +} \ No newline at end of file diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index aab001b..682ecdf 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -1,6 +1,11 @@ -namespace PMSWPF.ViewModels; +using CommunityToolkit.Mvvm.ComponentModel; -public class ViewModelBase +namespace PMSWPF.ViewModels; + +public partial class ViewModelBase:ObservableObject { - + public ViewModelBase() + { + + } } \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 4d01eb0..75155d4 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -4,12 +4,16 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:PMSWPF.Views" + xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs" + xmlns:vm="clr-namespace:PMSWPF.ViewModels" + d:DataContext="{d:DesignInstance vm:DevicesViewModel}" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> - -