给DialogService从原来的接口注入,更改为RequestMessage消息通知
This commit is contained in:
@@ -36,7 +36,7 @@ public partial class App : Application
|
||||
|
||||
container.AddSingleton<NavgatorServices>();
|
||||
container.AddSingleton<DevicesRepositories>();
|
||||
container.AddSingleton<IDeviceDialogService, DeviceDialogService>();
|
||||
container.AddSingleton<DialogService>();
|
||||
container.AddSingleton<GrowlNotificationService>();
|
||||
container.AddSingleton<MainViewModel>();
|
||||
container.AddSingleton<HomeViewModel>();
|
||||
@@ -53,6 +53,7 @@ public partial class App : Application
|
||||
Services = container.BuildServiceProvider();
|
||||
// 启动服务
|
||||
Services.GetRequiredService<GrowlNotificationService>();
|
||||
Services.GetRequiredService<DialogService>();
|
||||
}
|
||||
|
||||
public new static App Current => (App)Application.Current;
|
||||
|
||||
@@ -4,21 +4,17 @@ namespace PMSWPF.Data;
|
||||
|
||||
public class DbContext
|
||||
{
|
||||
private static SqlSugarClient _db;
|
||||
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
if (_db == null)
|
||||
{
|
||||
var connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=pmswpf;";
|
||||
_db = new SqlSugarClient(new ConnectionConfig
|
||||
var _db = new SqlSugarClient(new ConnectionConfig
|
||||
{
|
||||
ConnectionString = connectionString,
|
||||
DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return _db;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Windows.Controls;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Extensions;
|
||||
using PMSWPF.Models;
|
||||
using SqlSugar;
|
||||
|
||||
namespace PMSWPF.Data.Repositories;
|
||||
@@ -13,13 +15,21 @@ public class MenuRepositories
|
||||
_db=DbContext.GetInstance();
|
||||
}
|
||||
|
||||
public async Task<List<DbMenu>> GetMenu()
|
||||
public async Task<List<MenuBean>> GetMenu()
|
||||
{
|
||||
return await _db.Queryable<DbMenu>().ToListAsync();
|
||||
// //无主键用法新:5.1.4.110
|
||||
// db.Queryable<Tree>().ToTree(it=>it.Child,it=>it.ParentId,0,it=>it.Id)//+4重载
|
||||
List<MenuBean> menus=new();
|
||||
var dbMenuList=await _db.Queryable<DbMenu>().ToTreeAsync(dm=>dm.Items,dm=>dm.ParentId,0);
|
||||
foreach (var item in dbMenuList)
|
||||
{
|
||||
menus.Add(item.CopyTo<MenuBean>());
|
||||
}
|
||||
return menus;
|
||||
}
|
||||
|
||||
public async Task<int> AddMenu(DbMenu dbMenu)
|
||||
public async Task<int> AddMenu(MenuBean menu)
|
||||
{
|
||||
return await _db.Insertable<DbMenu>(dbMenu).ExecuteCommandAsync();
|
||||
return await _db.Insertable<DbMenu>(menu.CopyTo<DbMenu>()).ExecuteCommandAsync();
|
||||
}
|
||||
}
|
||||
9
Message/DialogMessage.cs
Normal file
9
Message/DialogMessage.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace PMSWPF.Message;
|
||||
|
||||
public class DialogMessage
|
||||
{
|
||||
public bool IsCancel { get; set; }
|
||||
public bool IsConfirm { get; set; }
|
||||
public Object? Request { get; set; }
|
||||
public Object? Response { get; set; }
|
||||
}
|
||||
21
Message/OpenDialogMessage.cs
Normal file
21
Message/OpenDialogMessage.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using CommunityToolkit.Mvvm.Messaging.Messages;
|
||||
|
||||
namespace PMSWPF.Message;
|
||||
|
||||
public class OpenDialogMessage:RequestMessage<DialogMessage>
|
||||
{
|
||||
// public bool IsCancel { get; set; }
|
||||
// public bool IsConfirm { get; set; }
|
||||
// public Object? Request { get; set; }
|
||||
// public Object? Response { get; set; }
|
||||
public DialogMessage Message { get; set; }
|
||||
|
||||
public OpenDialogMessage()
|
||||
{
|
||||
}
|
||||
|
||||
public OpenDialogMessage(DialogMessage message)
|
||||
{
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
11
Models/MenuBean.cs
Normal file
11
Models/MenuBean.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PMSWPF.Models;
|
||||
|
||||
public class MenuBean
|
||||
{
|
||||
|
||||
public int Id { get; set; }
|
||||
public string Icon { get; set; }
|
||||
public string Name { get; set; }
|
||||
public int ParentId { get; set; }
|
||||
public List<MenuBean> Items { get; set; }
|
||||
}
|
||||
@@ -1,12 +1,22 @@
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using HandyControl.Tools.Extension;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
using PMSWPF.Views.Dialogs;
|
||||
|
||||
namespace PMSWPF.Services;
|
||||
|
||||
public class DeviceDialogService : IDeviceDialogService
|
||||
public class DialogService:ObservableRecipient,IRecipient<OpenDialogMessage>
|
||||
{
|
||||
public DialogService()
|
||||
{
|
||||
IsActive = true;
|
||||
|
||||
}
|
||||
|
||||
public async Task<Device> ShowAddDeviceDialog()
|
||||
{
|
||||
var device = new Device();
|
||||
@@ -26,4 +36,11 @@ public class DeviceDialogService : IDeviceDialogService
|
||||
{
|
||||
MessageBox.Show(message);
|
||||
}
|
||||
|
||||
|
||||
public void Receive(OpenDialogMessage message)
|
||||
{
|
||||
|
||||
message.Reply(new DialogMessage(){IsConfirm = true, IsCancel = false});
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Enums;
|
||||
using PMSWPF.Excptions;
|
||||
using PMSWPF.Helper;
|
||||
using PMSWPF.Message;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
|
||||
@@ -13,17 +16,16 @@ namespace PMSWPF.ViewModels;
|
||||
|
||||
public partial class DevicesViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDeviceDialogService _deviceDialogService;
|
||||
private readonly DevicesRepositories _devicesRepositories;
|
||||
private readonly ILogger<DevicesViewModel> _logger;
|
||||
|
||||
[ObservableProperty] private ObservableCollection<Device> _devices;
|
||||
|
||||
public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,
|
||||
public DevicesViewModel(DevicesRepositories devicesRepositories,
|
||||
ILogger<DevicesViewModel> logger
|
||||
)
|
||||
{
|
||||
_deviceDialogService = deviceDialogService;
|
||||
|
||||
_devicesRepositories = devicesRepositories;
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -40,19 +42,31 @@ public partial class DevicesViewModel : ViewModelBase
|
||||
Device device = null;
|
||||
try
|
||||
{
|
||||
device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||
if (device != null)
|
||||
{
|
||||
var isOk = await _devicesRepositories.Add(device);
|
||||
if (isOk)
|
||||
{
|
||||
// MessageBox.Show("Device added successfully");
|
||||
await OnLoadedAsync();
|
||||
var msg = $"设备添加成功:{device.Name}";
|
||||
_logger.LogInformation(msg);
|
||||
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
}
|
||||
}
|
||||
OpenDialogMessage dialog = new OpenDialogMessage();
|
||||
|
||||
var res=WeakReferenceMessenger.Default.Send<OpenDialogMessage>(dialog);
|
||||
|
||||
Console.WriteLine("");
|
||||
|
||||
// device = await _deviceDialogService.ShowAddDeviceDialog();
|
||||
// if (device != null)
|
||||
// {
|
||||
// var isOk = await _devicesRepositories.Add(device);
|
||||
// if (isOk)
|
||||
// {
|
||||
// // 添加菜单项
|
||||
// MenuBean deviceMenu = new MenuBean()
|
||||
// { Name = device.Name, Icon = SegoeFluentIcons.Devices4.Glyph, ParentId = 2 };
|
||||
// MenuRepositories mre = new MenuRepositories();
|
||||
// mre.AddMenu(deviceMenu);
|
||||
//
|
||||
// // MessageBox.Show("Device added successfully");
|
||||
// await OnLoadedAsync();
|
||||
// var msg = $"设备添加成功:{device.Name}";
|
||||
// _logger.LogInformation(msg);
|
||||
// NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
catch (DbExistException e)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PMSWPF.Data.Entities;
|
||||
using PMSWPF.Data.Repositories;
|
||||
using PMSWPF.Models;
|
||||
using PMSWPF.Services;
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
@@ -12,7 +14,7 @@ public partial class MainViewModel : ViewModelBase
|
||||
|
||||
[ObservableProperty] private ViewModelBase currentViewModel;
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<DbMenu> _menus;
|
||||
private ObservableCollection<MenuBean> _menus;
|
||||
|
||||
public MainViewModel(NavgatorServices navgatorServices)
|
||||
{
|
||||
@@ -23,10 +25,11 @@ public partial class MainViewModel : ViewModelBase
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override async void OnLoaded()
|
||||
{
|
||||
MenuRepositories mr = new MenuRepositories();
|
||||
var menuList= await mr.GetMenu();
|
||||
Menus=new ObservableCollection<DbMenu>(menuList);
|
||||
Menus=new ObservableCollection<MenuBean>(menuList);
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,25 @@
|
||||
xmlns:local="clr-namespace:PMSWPF.Views"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
||||
xmlns:mo="clr-namespace:PMSWPF.Models"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
Title="设备管理系统"
|
||||
Width="1080"
|
||||
Height="800"
|
||||
|
||||
Loaded="MainView_OnLoaded"
|
||||
ui:WindowHelper.UseModernWindowStyle="True"
|
||||
ui:WindowHelper.SystemBackdropType="Mica"
|
||||
d:DataContext="{d:DesignInstance vm:MainViewModel}"
|
||||
mc:Ignorable="d">
|
||||
<Window.Resources>
|
||||
<DataTemplate x:Key="NavigationViewMenuItem" DataType="{x:Type mo:MenuBean}">
|
||||
<ui:NavigationViewItem Content="{Binding Name}" MenuItemsSource="{Binding Items }">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Glyph="{Binding Icon}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
</DataTemplate>
|
||||
</Window.Resources>
|
||||
<Grid>
|
||||
<ui:NavigationView
|
||||
|
||||
@@ -28,42 +38,45 @@
|
||||
IsBackButtonVisible="Collapsed"
|
||||
IsBackEnabled="False"
|
||||
SelectionFollowsFocus="Disabled"
|
||||
SelectionChanged="NavigationView_SelectionChanged">
|
||||
MenuItemsSource="{Binding Menus}"
|
||||
MenuItemTemplate="{StaticResource NavigationViewMenuItem}"
|
||||
ItemInvoked="NavigationView_OnItemInvoked"
|
||||
>
|
||||
|
||||
<ui:NavigationView.MenuItems>
|
||||
<ui:NavigationViewItem
|
||||
Content="主页"
|
||||
Tag="Home">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Home}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
<ui:NavigationViewItem
|
||||
Content="设备"
|
||||
Tag="Devices">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Devices2}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
<ui:NavigationViewItem
|
||||
Content="数据转换"
|
||||
Tag="DataTransform">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
<ui:NavigationViewItem Tag="Setting" Content="设置">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Settings}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
<ui:NavigationViewItem Content="关于">
|
||||
<ui:NavigationViewItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Info}" />
|
||||
</ui:NavigationViewItem.Icon>
|
||||
</ui:NavigationViewItem>
|
||||
|
||||
</ui:NavigationView.MenuItems>
|
||||
<!-- <ui:NavigationView.MenuItems> -->
|
||||
<!-- <ui:NavigationViewItem -->
|
||||
<!-- Content="主页" -->
|
||||
<!-- Tag="Home"> -->
|
||||
<!-- <ui:NavigationViewItem.Icon> -->
|
||||
<!-- <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Home}" /> -->
|
||||
<!-- </ui:NavigationViewItem.Icon> -->
|
||||
<!-- </ui:NavigationViewItem> -->
|
||||
<!-- <ui:NavigationViewItem -->
|
||||
<!-- Content="设备" -->
|
||||
<!-- Tag="Devices"> -->
|
||||
<!-- <ui:NavigationViewItem.Icon> -->
|
||||
<!-- <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Devices2}" /> -->
|
||||
<!-- </ui:NavigationViewItem.Icon> -->
|
||||
<!-- </ui:NavigationViewItem> -->
|
||||
<!-- <ui:NavigationViewItem -->
|
||||
<!-- Content="数据转换" -->
|
||||
<!-- Tag="DataTransform"> -->
|
||||
<!-- <ui:NavigationViewItem.Icon> -->
|
||||
<!-- <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Switch}" /> -->
|
||||
<!-- </ui:NavigationViewItem.Icon> -->
|
||||
<!-- </ui:NavigationViewItem> -->
|
||||
<!-- <ui:NavigationViewItem Tag="Setting" Content="设置"> -->
|
||||
<!-- <ui:NavigationViewItem.Icon> -->
|
||||
<!-- <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Settings}" /> -->
|
||||
<!-- </ui:NavigationViewItem.Icon> -->
|
||||
<!-- </ui:NavigationViewItem> -->
|
||||
<!-- <ui:NavigationViewItem Content="关于"> -->
|
||||
<!-- <ui:NavigationViewItem.Icon> -->
|
||||
<!-- <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Info}" /> -->
|
||||
<!-- </ui:NavigationViewItem.Icon> -->
|
||||
<!-- </ui:NavigationViewItem> -->
|
||||
<!-- -->
|
||||
<!-- </ui:NavigationView.MenuItems> -->
|
||||
|
||||
<ui:NavigationView.AutoSuggestBox>
|
||||
<ui:AutoSuggestBox AutomationProperties.Name="Search">
|
||||
|
||||
@@ -14,11 +14,12 @@ namespace PMSWPF.Views;
|
||||
public partial class MainView : Window
|
||||
{
|
||||
private readonly ILogger<MainView> _logger;
|
||||
|
||||
private MainViewModel _viewModel;
|
||||
public MainView(MainViewModel viewModel, ILogger<MainView> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
InitializeComponent();
|
||||
_viewModel = viewModel;
|
||||
_logger = logger;
|
||||
DataContext = viewModel;
|
||||
_logger.LogInformation("主界面加载成功");
|
||||
}
|
||||
@@ -59,4 +60,40 @@ public partial class MainView : Window
|
||||
var nm = new NavgatorMessage(navgateVM);
|
||||
WeakReferenceMessenger.Default.Send(nm);
|
||||
}
|
||||
|
||||
private async void MainView_OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_viewModel.OnLoaded();
|
||||
}
|
||||
|
||||
private void NavigationView_OnItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
|
||||
{
|
||||
ViewModelBase navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
||||
switch (args.InvokedItem)
|
||||
{
|
||||
case "主页":
|
||||
// mainViewModel.NavgateTo<HomeViewModel>();
|
||||
navgateVM = App.Current.Services.GetRequiredService<HomeViewModel>();
|
||||
_logger.LogInformation("导航到到主页面");
|
||||
break;
|
||||
case "设备":
|
||||
navgateVM = App.Current.Services.GetRequiredService<DevicesViewModel>();
|
||||
// mainViewModel.NavgateTo<DevicesViewModel>();
|
||||
_logger.LogInformation("导航到到设备页面");
|
||||
break;
|
||||
case "数据转换":
|
||||
navgateVM = App.Current.Services.GetRequiredService<DataTransformViewModel>();
|
||||
// mainViewModel.NavgateTo<DataTransformViewModel>();
|
||||
_logger.LogInformation("导航到到数据转换页面");
|
||||
break;
|
||||
case "设置":
|
||||
// mainViewModel.NavgateTo<SettingViewModel>();
|
||||
navgateVM = App.Current.Services.GetRequiredService<SettingViewModel>();
|
||||
_logger.LogInformation("导航到到设备页面");
|
||||
break;
|
||||
}
|
||||
|
||||
var nm = new NavgatorMessage(navgateVM);
|
||||
WeakReferenceMessenger.Default.Send(nm);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user