添加后台服务是否启动功能
This commit is contained in:
@@ -13,6 +13,7 @@ using PMSWPF.Services;
|
||||
using PMSWPF.ViewModels;
|
||||
using PMSWPF.Views;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using PMSWPF.Config;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
using SqlSugar;
|
||||
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
||||
@@ -81,8 +82,14 @@ public partial class App : Application
|
||||
services.AddSingleton<NavgatorServices>();
|
||||
services.AddSingleton<IDialogService, DialogService>();
|
||||
services.AddSingleton<GrowlNotificationService>();
|
||||
if (ConnectionSettings.Load().EnableS7Service)
|
||||
{
|
||||
services.AddHostedService<S7BackgroundService>(); // Register as HostedService
|
||||
}
|
||||
if (ConnectionSettings.Load().EnableMqttService)
|
||||
{
|
||||
services.AddHostedService<MqttBackgroundService>();
|
||||
}
|
||||
services.AddSingleton<MainViewModel>();
|
||||
services.AddSingleton<HomeViewModel>();
|
||||
services.AddSingleton<DevicesViewModel>();
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace PMSWPF.Config
|
||||
public string UserId { get; set; } = "root";
|
||||
public string Password { get; set; } = "Pgw15221236646";
|
||||
public string Database { get; set; } = "pmswpf";
|
||||
public bool EnableS7Service { get; set; } = true;
|
||||
public bool EnableMqttService { get; set; } = true;
|
||||
|
||||
private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "connectionSettings.json");
|
||||
|
||||
|
||||
28
ViewModels/Dialogs/IsActiveDialogViewModel.cs
Normal file
28
ViewModels/Dialogs/IsActiveDialogViewModel.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PMSWPF.Enums;
|
||||
|
||||
namespace PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
public partial class IsActiveDialogViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool? _selectedIsActive;
|
||||
|
||||
public IsActiveDialogViewModel(bool? currentIsActive)
|
||||
{
|
||||
_selectedIsActive = currentIsActive;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectIsActive(bool isActive)
|
||||
{
|
||||
SelectedIsActive = isActive;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
SelectedIsActive = null;
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,34 @@ public partial class SettingViewModel : ViewModelBase
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnableS7Service
|
||||
{
|
||||
get => _connectionSettings.EnableS7Service;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.EnableS7Service != value)
|
||||
{
|
||||
_connectionSettings.EnableS7Service = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool EnableMqttService
|
||||
{
|
||||
get => _connectionSettings.EnableMqttService;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.EnableMqttService != value)
|
||||
{
|
||||
_connectionSettings.EnableMqttService = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task TestConnection()
|
||||
{
|
||||
|
||||
22
Views/Dialogs/IsActiveDialog.xaml
Normal file
22
Views/Dialogs/IsActiveDialog.xaml
Normal file
@@ -0,0 +1,22 @@
|
||||
<controls:ContentDialog x:Class="PMSWPF.Views.Dialogs.IsActiveDialog"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels.Dialogs"
|
||||
mc:Ignorable="d"
|
||||
d:DataContext="{d:DesignInstance vm:IsActiveDialogViewModel}"
|
||||
Title="修改激活状态"
|
||||
PrimaryButtonText="确定"
|
||||
SecondaryButtonText="取消"
|
||||
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
|
||||
SecondaryButtonClick="ContentDialog_SecondaryButtonClick">
|
||||
<Grid>
|
||||
<StackPanel Orientation="Vertical" Margin="10">
|
||||
<TextBlock Text="请选择新的激活状态:" Margin="0,0,0,10" FontWeight="Bold"/>
|
||||
<RadioButton Content="启用" IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=True}" Command="{Binding SelectIsActiveCommand}" CommandParameter="True" Margin="0,0,0,5"/>
|
||||
<RadioButton Content="停用" IsChecked="{Binding SelectedIsActive, Mode=TwoWay, Converter={StaticResource NullableBooleanConverter}, ConverterParameter=False}" Command="{Binding SelectIsActiveCommand}" CommandParameter="False"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</controls:ContentDialog>
|
||||
33
Views/Dialogs/IsActiveDialog.xaml.cs
Normal file
33
Views/Dialogs/IsActiveDialog.xaml.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Windows;
|
||||
using iNKORE.UI.WPF.Modern.Controls;
|
||||
using PMSWPF.ViewModels.Dialogs;
|
||||
|
||||
namespace PMSWPF.Views.Dialogs;
|
||||
|
||||
public partial class IsActiveDialog : ContentDialog
|
||||
{
|
||||
public IsActiveDialogViewModel ViewModel { get; }
|
||||
|
||||
public IsActiveDialog(IsActiveDialogViewModel viewModel)
|
||||
{
|
||||
InitializeComponent();
|
||||
ViewModel = viewModel;
|
||||
DataContext = ViewModel;
|
||||
}
|
||||
|
||||
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
{
|
||||
// 确认按钮点击时,ViewModel.SelectedIsActive 已经通过绑定更新
|
||||
// 如果用户没有选择任何选项,可以阻止关闭对话框
|
||||
if (!ViewModel.SelectedIsActive.HasValue)
|
||||
{
|
||||
args.Cancel = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||
{
|
||||
// 取消按钮点击时,将 SelectedIsActive 设置为 null
|
||||
ViewModel.SelectedIsActive = null;
|
||||
}
|
||||
}
|
||||
@@ -108,6 +108,25 @@
|
||||
|
||||
</ui:SettingsExpander>
|
||||
|
||||
<ui:SettingsExpander Header="后台服务"
|
||||
Description="后台服务相关配置">
|
||||
<ui:SettingsExpander.HeaderIcon>
|
||||
<ui:FontIcon Glyph="" />
|
||||
</ui:SettingsExpander.HeaderIcon>
|
||||
<ui:SettingsExpander.Items>
|
||||
<ui:SettingsCard Header="是否启动S7后台服务"
|
||||
Description="关闭后,将不再轮询S7设备"
|
||||
IsClickEnabled="True">
|
||||
<ui:ToggleSwitch IsOn="{Binding EnableS7Service, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard Header="是否启动Mqtt后台服务"
|
||||
Description="关闭后,将不再启动Mqtt服务"
|
||||
IsClickEnabled="True">
|
||||
<ui:ToggleSwitch IsOn="{Binding EnableMqttService, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ui:SettingsCard>
|
||||
</ui:SettingsExpander.Items>
|
||||
</ui:SettingsExpander>
|
||||
|
||||
<ui:SettingsExpander Header="SettingsExpander"
|
||||
Description="The SettingsExpander has the same properties as a SettingsCard">
|
||||
<ui:SettingsExpander.HeaderIcon>
|
||||
|
||||
Reference in New Issue
Block a user