完成设备界面的简单布局,引入GridView的使用,并接GridView的模板抽离成Dictionary

This commit is contained in:
2025-06-13 18:54:17 +08:00
parent 080b9f0aa9
commit fd68a223fd
13 changed files with 117 additions and 11 deletions

View File

@@ -10,7 +10,10 @@
<ResourceDictionary.MergedDictionaries>
<ui:ThemeResources/>
<ui:XamlControlsResources/>
<ResourceDictionary Source="Resources/DevicesItemTemplateDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

View File

@@ -0,0 +1,19 @@
namespace PMSWPF.Extensions;
public static class TaskExtensions
{
public static async Task Await(this Task task,Action<Exception> onError=null,Action onComplete=null)
{
try
{
await task;
onComplete?.Invoke();
}
catch (Exception e)
{
onError?.Invoke(e);
}
}
}

View File

@@ -17,8 +17,5 @@
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178" />
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,16 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="DeviceItemTemplate">
<Border Background="#eee" CornerRadius="10" Padding="10" >
<ikw:SimpleStackPanel Spacing="10" Width="300" Height="200">
<TextBlock FontSize="26" FontWeight="Bold" Text="{Binding Name }" />
<TextBlock Text="{Binding Description }" />
<TextBlock Text="{Binding Ip }" />
</ikw:SimpleStackPanel>
</Border>
</DataTemplate>
</ResourceDictionary>

View File

@@ -27,4 +27,9 @@ public class DeviceDialogService : IDeviceDialogService
}
}
public void ShowMessageDialog(string title, string message)
{
MessageBox.Show(message);
}
}

View File

@@ -5,4 +5,6 @@ namespace PMSWPF.Services;
public interface IDeviceDialogService
{
Task<Device> ShowAddDeviceDialog();
void ShowMessageDialog(string title, string message);
}

View File

@@ -16,6 +16,7 @@ public class NavgatorServices
{
currentViewModel = value;
OnViewModelChanged?.Invoke();
currentViewModel.OnLoaded();
}
}

View File

@@ -2,5 +2,8 @@
public class DataTransformViewModel:ViewModelBase
{
public override void OnLoaded()
{
}
}

View File

@@ -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<Device> _devices;
public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories)
{
_deviceDialogService = deviceDialogService;
_devicesRepositories = devicesRepositories;
}
public async Task OnLoadedAsync()
{
var ds = await _devicesRepositories.GetAll();
_devices = new ObservableCollection<Device>();
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>(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);
}, () =>
{
});
}
}

View File

@@ -2,5 +2,8 @@
public class HomeViewModel:ViewModelBase
{
public override void OnLoaded()
{
}
}

View File

@@ -2,10 +2,12 @@
namespace PMSWPF.ViewModels;
public partial class ViewModelBase:ObservableObject
public abstract partial class ViewModelBase:ObservableObject
{
public ViewModelBase()
{
}
public abstract void OnLoaded();
}

View File

@@ -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 @@
<StackPanel Margin="10 5" Orientation="Horizontal">
<Button Margin="5" Command="{Binding AddDeviceCommand}" Content="添加"/>
</StackPanel>
<TextBlock Text="DevicesView" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<ui:GridView x:Name="BasicGridView"
Margin="10"
IsItemClickEnabled="True"
ItemClick="BasicGridView_ItemClick"
ItemsSource="{Binding Devices}"
ItemTemplate="{StaticResource DeviceItemTemplate}"
SelectionMode="Single" />
</StackPanel>
</UserControl>

View File

@@ -1,4 +1,7 @@
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls;
using iNKORE.UI.WPF.Modern.Controls;
using PMSWPF.ViewModels;
namespace PMSWPF.Views;
@@ -8,4 +11,15 @@ public partial class DevicesView : UserControl
{
InitializeComponent();
}
private void BasicGridView_ItemClick(object sender, ItemClickEventArgs e)
{
}
private async void DevicesView_OnLoaded(object sender, RoutedEventArgs e)
{
// var devicesViewModel = (DevicesViewModel)this.DataContext;
// await devicesViewModel.OnLoadedAsync();
}
}