完成设备界面的简单布局,引入GridView的使用,并接GridView的模板抽离成Dictionary
This commit is contained in:
3
App.xaml
3
App.xaml
@@ -10,7 +10,10 @@
|
|||||||
<ResourceDictionary.MergedDictionaries>
|
<ResourceDictionary.MergedDictionaries>
|
||||||
<ui:ThemeResources/>
|
<ui:ThemeResources/>
|
||||||
<ui:XamlControlsResources/>
|
<ui:XamlControlsResources/>
|
||||||
|
<ResourceDictionary Source="Resources/DevicesItemTemplateDictionary.xaml" />
|
||||||
</ResourceDictionary.MergedDictionaries>
|
</ResourceDictionary.MergedDictionaries>
|
||||||
|
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
|
|
||||||
</Application.Resources>
|
</Application.Resources>
|
||||||
</Application>
|
</Application>
|
||||||
|
|||||||
19
Extensions/TaskExtensions.cs
Normal file
19
Extensions/TaskExtensions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,8 +17,5 @@
|
|||||||
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178" />
|
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178" />
|
||||||
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193" />
|
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Resources\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
16
Resources/DevicesItemTemplateDictionary.xaml
Normal file
16
Resources/DevicesItemTemplateDictionary.xaml
Normal 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>
|
||||||
@@ -27,4 +27,9 @@ public class DeviceDialogService : IDeviceDialogService
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ShowMessageDialog(string title, string message)
|
||||||
|
{
|
||||||
|
MessageBox.Show(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,4 +5,6 @@ namespace PMSWPF.Services;
|
|||||||
public interface IDeviceDialogService
|
public interface IDeviceDialogService
|
||||||
{
|
{
|
||||||
Task<Device> ShowAddDeviceDialog();
|
Task<Device> ShowAddDeviceDialog();
|
||||||
|
|
||||||
|
void ShowMessageDialog(string title, string message);
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,7 @@ public class NavgatorServices
|
|||||||
{
|
{
|
||||||
currentViewModel = value;
|
currentViewModel = value;
|
||||||
OnViewModelChanged?.Invoke();
|
OnViewModelChanged?.Invoke();
|
||||||
|
currentViewModel.OnLoaded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,5 +2,8 @@
|
|||||||
|
|
||||||
public class DataTransformViewModel:ViewModelBase
|
public class DataTransformViewModel:ViewModelBase
|
||||||
{
|
{
|
||||||
|
public override void OnLoaded()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Windows;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using PMSWPF.Data.Entities;
|
using PMSWPF.Data.Entities;
|
||||||
using PMSWPF.Data.Repositories;
|
using PMSWPF.Data.Repositories;
|
||||||
@@ -16,13 +18,27 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
private readonly IDeviceDialogService _deviceDialogService;
|
private readonly IDeviceDialogService _deviceDialogService;
|
||||||
private readonly DevicesRepositories _devicesRepositories;
|
private readonly DevicesRepositories _devicesRepositories;
|
||||||
|
[ObservableProperty]
|
||||||
|
private ObservableCollection<Device> _devices;
|
||||||
public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories)
|
public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories)
|
||||||
{
|
{
|
||||||
_deviceDialogService = deviceDialogService;
|
_deviceDialogService = deviceDialogService;
|
||||||
_devicesRepositories = devicesRepositories;
|
_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]
|
[RelayCommand]
|
||||||
public async void AddDevice()
|
public async void AddDevice()
|
||||||
@@ -35,7 +51,11 @@ public partial class DevicesViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
DbDevice dbDevice = new DbDevice();
|
DbDevice dbDevice = new DbDevice();
|
||||||
device.CopyTo<DbDevice>(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);
|
MessageBox.Show(e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void OnLoaded()
|
||||||
|
{
|
||||||
|
OnLoadedAsync().Await((e) =>
|
||||||
|
{
|
||||||
|
_deviceDialogService.ShowMessageDialog("",e.Message);
|
||||||
|
}, () =>
|
||||||
|
{
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,8 @@
|
|||||||
|
|
||||||
public class HomeViewModel:ViewModelBase
|
public class HomeViewModel:ViewModelBase
|
||||||
{
|
{
|
||||||
|
public override void OnLoaded()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
namespace PMSWPF.ViewModels;
|
namespace PMSWPF.ViewModels;
|
||||||
|
|
||||||
public partial class ViewModelBase:ObservableObject
|
public abstract partial class ViewModelBase:ObservableObject
|
||||||
{
|
{
|
||||||
public ViewModelBase()
|
public ViewModelBase()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void OnLoaded();
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,11 @@
|
|||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
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:local="clr-namespace:PMSWPF.Views"
|
||||||
xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs"
|
xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs"
|
||||||
|
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
|
||||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
||||||
d:DataContext="{d:DesignInstance vm:DevicesViewModel}"
|
d:DataContext="{d:DesignInstance vm:DevicesViewModel}"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
@@ -13,7 +16,14 @@
|
|||||||
<StackPanel Margin="10 5" Orientation="Horizontal">
|
<StackPanel Margin="10 5" Orientation="Horizontal">
|
||||||
<Button Margin="5" Command="{Binding AddDeviceCommand}" Content="添加"/>
|
<Button Margin="5" Command="{Binding AddDeviceCommand}" Content="添加"/>
|
||||||
</StackPanel>
|
</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>
|
</StackPanel>
|
||||||
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|||||||
@@ -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;
|
namespace PMSWPF.Views;
|
||||||
|
|
||||||
@@ -8,4 +11,15 @@ public partial class DevicesView : UserControl
|
|||||||
{
|
{
|
||||||
InitializeComponent();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user