完成设备的启用和停用并更新界面
This commit is contained in:
85
DMS.WPF/Converters/BooleanToBrushConverter.cs
Normal file
85
DMS.WPF/Converters/BooleanToBrushConverter.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace DMS.WPF.Converters
|
||||
{
|
||||
public class BooleanToBrushConverter : IValueConverter
|
||||
{
|
||||
// Predefined "True" color
|
||||
private static readonly Color DefaultTrueColor = Color.FromArgb(0xFF, 0xA3, 0xE4, 0xD7); // Mint Green
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is bool boolValue)
|
||||
{
|
||||
// If parameter is provided, try to use it as the "True" color
|
||||
string param = parameter as string;
|
||||
if (!string.IsNullOrEmpty(param))
|
||||
{
|
||||
// Split the parameter by '|' to see if it contains two colors
|
||||
string[] colors = param.Split('|');
|
||||
|
||||
try
|
||||
{
|
||||
if (colors.Length == 2)
|
||||
{
|
||||
// Two colors: TrueColor|FalseColor
|
||||
Color trueColor = (Color)ColorConverter.ConvertFromString(colors[0]);
|
||||
if (colors[1].Equals("Default", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// For false, return UnsetValue to let the control use its default background
|
||||
return boolValue ? new SolidColorBrush(trueColor) :
|
||||
System.Windows.DependencyProperty.UnsetValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
Color falseColor = (Color)ColorConverter.ConvertFromString(colors[1]);
|
||||
return boolValue ? new SolidColorBrush(trueColor) :
|
||||
new SolidColorBrush(falseColor);
|
||||
}
|
||||
}
|
||||
else if (colors.Length == 1)
|
||||
{
|
||||
// One color: TrueColor
|
||||
Color trueColor = (Color)ColorConverter.ConvertFromString(colors[0]);
|
||||
if (boolValue)
|
||||
{
|
||||
return new SolidColorBrush(trueColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
// For false, return UnsetValue to let the control use its default background
|
||||
return System.Windows.DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// If color format is invalid, fall back to default colors
|
||||
}
|
||||
}
|
||||
|
||||
// Default behavior
|
||||
if (boolValue)
|
||||
{
|
||||
return new SolidColorBrush(DefaultTrueColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
// For false, return UnsetValue to let the control use its default background
|
||||
return System.Windows.DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
|
||||
// If value is not a boolean, return UnsetValue
|
||||
return System.Windows.DependencyProperty.UnsetValue;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Threading;
|
||||
using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.WPF.Interfaces;
|
||||
using DMS.WPF.ViewModels.Items;
|
||||
@@ -17,22 +19,54 @@ public class DeviceDataService : IDeviceDataService
|
||||
private readonly IAppDataCenterService _appDataCenterService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IDataStorageService _dataStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly INotificationService _notificationService;
|
||||
private readonly IMenuDataService _menuDataService;
|
||||
private readonly IVariableDataService _variableDataService;
|
||||
private readonly Dispatcher _uiDispatcher;
|
||||
|
||||
/// <summary>
|
||||
/// DeviceDataService类的构造函数。
|
||||
/// </summary>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
/// <param name="appDataCenterService">数据服务中心实例。</param>
|
||||
public DeviceDataService(IMapper mapper, IAppDataCenterService appDataCenterService,IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService,IMenuDataService menuDataService,IVariableDataService variableDataService)
|
||||
public DeviceDataService(IMapper mapper, IAppDataCenterService appDataCenterService,
|
||||
IAppDataStorageService appDataStorageService, IDataStorageService dataStorageService,
|
||||
IEventService eventService,INotificationService notificationService,
|
||||
IMenuDataService menuDataService, IVariableDataService variableDataService)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_appDataCenterService = appDataCenterService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_dataStorageService = dataStorageService;
|
||||
_eventService = eventService;
|
||||
_notificationService = notificationService;
|
||||
_menuDataService = menuDataService;
|
||||
_variableDataService = variableDataService;
|
||||
_uiDispatcher = Dispatcher.CurrentDispatcher;
|
||||
|
||||
_eventService.OnDeviceConnectChanged += OnDeviceConnectChanged;
|
||||
}
|
||||
|
||||
private void OnDeviceConnectChanged(object? sender, DeviceConnectChangedEventArgs e)
|
||||
{
|
||||
_uiDispatcher.Invoke(() =>
|
||||
{
|
||||
var device = _dataStorageService.Devices.FirstOrDefault(d => d.Id == e.DeviceId);
|
||||
if (device != null)
|
||||
{
|
||||
|
||||
device.IsRunning = e.NewStatus;
|
||||
if (device.IsRunning)
|
||||
{
|
||||
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
|
||||
}
|
||||
else
|
||||
{
|
||||
_notificationService.ShowSuccess($"设备:{device.Name},已断开连接。");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,7 +78,6 @@ public class DeviceDataService : IDeviceDataService
|
||||
{
|
||||
_dataStorageService.Devices.Add(_mapper.Map<DeviceItemViewModel>(deviceDto));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -78,6 +78,11 @@ public partial class DeviceItemViewModel : ObservableObject
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnIsRunningChanged(bool oldValue, bool newValue)
|
||||
{
|
||||
System.Console.WriteLine($"IsRunning changed from {oldValue} to {newValue} for device {Name}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当IsActive属性改变时调用,用于发布设备状态改变事件
|
||||
/// </summary>
|
||||
|
||||
@@ -4,23 +4,24 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
|
||||
xmlns:localConverters="clr-namespace:DMS.WPF.Converters"
|
||||
d:DataContext="{d:DesignInstance vm:DevicesViewModel}"
|
||||
d:DesignHeight="300"
|
||||
d:DesignWidth="300"
|
||||
mc:Ignorable="d">
|
||||
<UserControl.Resources>
|
||||
<localConverters:BooleanToBrushConverter x:Key="BooleanToBrushConverter" />
|
||||
<DataTemplate x:Key="DeviceItemTemplate">
|
||||
<Border
|
||||
Margin="5"
|
||||
Padding="15"
|
||||
Background="{DynamicResource SystemControlBackgroundAltHighBrush}"
|
||||
BorderBrush="{DynamicResource SystemControlHighlightBaseMediumLowBrush}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8">
|
||||
CornerRadius="8"
|
||||
Background="{Binding IsRunning, Converter={StaticResource BooleanToBrushConverter}, ConverterParameter='#FFA8E063|{DynamicResource SystemControlBackgroundAltHighBrush}'}">
|
||||
<Border.Effect>
|
||||
<DropShadowEffect
|
||||
BlurRadius="5"
|
||||
@@ -28,16 +29,8 @@
|
||||
ShadowDepth="1"
|
||||
Color="Black" />
|
||||
</Border.Effect>
|
||||
<Border.Style>
|
||||
<Style TargetType="Border">
|
||||
<Style.Triggers>
|
||||
<DataTrigger Binding="{Binding IsRunning}" Value="True">
|
||||
<Setter Property="Background" Value="Aquamarine" />
|
||||
</DataTrigger>
|
||||
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Border.Style>
|
||||
|
||||
|
||||
|
||||
<Grid>
|
||||
@@ -59,6 +52,7 @@
|
||||
FontSize="20"
|
||||
FontWeight="SemiBold"
|
||||
Text="{Binding Name}" />
|
||||
|
||||
</DockPanel>
|
||||
|
||||
<!-- Row 1: Details with Icons -->
|
||||
|
||||
Reference in New Issue
Block a user