This commit is contained in:
39
Converters/DoubleStringConverter.cs
Normal file
39
Converters/DoubleStringConverter.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Windows.Data;
|
||||||
|
|
||||||
|
namespace MembranePoreTester.Converters
|
||||||
|
{
|
||||||
|
// 将 double 与 string 之间进行安全转换,避免在输入未完成时把值重置为 0
|
||||||
|
public class DoubleStringConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value == null) return string.Empty;
|
||||||
|
if (value is double d)
|
||||||
|
{
|
||||||
|
return d.ToString("G", culture);
|
||||||
|
}
|
||||||
|
return value.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
var s = (value as string) ?? string.Empty;
|
||||||
|
s = s.Trim();
|
||||||
|
if (string.IsNullOrEmpty(s))
|
||||||
|
{
|
||||||
|
// 不强制将空字符串写回为0,避免用户输入时被覆盖
|
||||||
|
return Binding.DoNothing;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (double.TryParse(s, NumberStyles.Any, culture, out double result))
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果解析失败,不更新源
|
||||||
|
return Binding.DoNothing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -251,7 +251,7 @@ namespace MembranePoreTester.ViewModels
|
|||||||
public ICommand RemoveDataPointCommand { get; }
|
public ICommand RemoveDataPointCommand { get; }
|
||||||
public ICommand CalculateCommand { get; }
|
public ICommand CalculateCommand { get; }
|
||||||
public ICommand GenerateReportCommand { get; }
|
public ICommand GenerateReportCommand { get; }
|
||||||
|
private System.Windows.Threading.DispatcherTimer _timer; // 添加定时器字段
|
||||||
public PoreDistributionViewModel()
|
public PoreDistributionViewModel()
|
||||||
{
|
{
|
||||||
_plcService = App.PlcService;
|
_plcService = App.PlcService;
|
||||||
@@ -272,11 +272,24 @@ namespace MembranePoreTester.ViewModels
|
|||||||
|
|
||||||
Record.DataPoints.CollectionChanged += (s, e) => UpdatePlot();
|
Record.DataPoints.CollectionChanged += (s, e) => UpdatePlot();
|
||||||
|
|
||||||
// 加到构造函数最后即可
|
|
||||||
ClearAllCommand = new RelayCommand(ClearAllData);
|
ClearAllCommand = new RelayCommand(ClearAllData);
|
||||||
|
|
||||||
RemoveDataPointCommand = new RelayCommand(RemoveSelectedDataPoint, () => SelectedDataPoint != null);
|
RemoveDataPointCommand = new RelayCommand(RemoveSelectedDataPoint, () => SelectedDataPoint != null);
|
||||||
|
|
||||||
|
|
||||||
|
// 启动定时器,每秒读取一次
|
||||||
|
_timer = new System.Windows.Threading.DispatcherTimer();
|
||||||
|
_timer.Interval = TimeSpan.FromSeconds(1);
|
||||||
|
_timer.Tick += async (s, e) =>
|
||||||
|
{
|
||||||
|
if (StationId > 0 && !IsDisposed)
|
||||||
|
{
|
||||||
|
await ReadSpeedRateAsync();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_timer.Start();
|
||||||
|
|
||||||
|
|
||||||
// 延迟2秒后读取,确保连接稳定
|
// 延迟2秒后读取,确保连接稳定
|
||||||
Task.Delay(2000).ContinueWith(async _ =>
|
Task.Delay(2000).ContinueWith(async _ =>
|
||||||
{
|
{
|
||||||
@@ -290,6 +303,38 @@ namespace MembranePoreTester.ViewModels
|
|||||||
ClearData();
|
ClearData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string _speedRate1;
|
||||||
|
public string SpeedRate1
|
||||||
|
{
|
||||||
|
get => _speedRate1;
|
||||||
|
set => SetProperty(ref _speedRate1, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private async Task ReadSpeedRateAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
float speedRate = 0;
|
||||||
|
switch (StationId)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
speedRate = await _plcService.ReadFloatAsync(_plcConfig.HPCoeff11); // 使用正确的地址
|
||||||
|
SpeedRate1 = speedRate.ToString("F3");
|
||||||
|
break;
|
||||||
|
// case 2,3 可类似添加
|
||||||
|
}
|
||||||
|
// 如果需要刷新 Record 中的绑定(可选)
|
||||||
|
OnPropertyChanged(nameof(SpeedRate1));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"读取加压速率失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task ReadPressureModeAsync()
|
private async Task ReadPressureModeAsync()
|
||||||
{
|
{
|
||||||
await SafeExecuteAsync($"ReadPressureModeAsync{StationId}", async () =>
|
await SafeExecuteAsync($"ReadPressureModeAsync{StationId}", async () =>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
xmlns:conv="clr-namespace:MembranePoreTester.Converters">
|
xmlns:conv="clr-namespace:MembranePoreTester.Converters">
|
||||||
<UserControl.Resources>
|
<UserControl.Resources>
|
||||||
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
|
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
|
||||||
|
<conv:DoubleStringConverter x:Key="DoubleStringConverter"/>
|
||||||
|
|
||||||
<Style TargetType="RadioButton">
|
<Style TargetType="RadioButton">
|
||||||
<Setter Property="Margin" Value="5,2"/>
|
<Setter Property="Margin" Value="5,2"/>
|
||||||
@@ -53,7 +54,7 @@
|
|||||||
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Record.RoomTemperature}" Margin="5"/>-->
|
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Record.RoomTemperature}" Margin="5"/>-->
|
||||||
|
|
||||||
<Label Grid.Row="1" Grid.Column="0" Content="浸润时间(h):"/>
|
<Label Grid.Row="1" Grid.Column="0" Content="浸润时间(h):"/>
|
||||||
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Record.SoakingTime}" Margin="5"/>
|
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Record.SoakingTime, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleStringConverter}}" Margin="5"/>
|
||||||
|
|
||||||
<Label Grid.Row="2" Grid.Column="0" Content="加压速率:"/>
|
<Label Grid.Row="2" Grid.Column="0" Content="加压速率:"/>
|
||||||
<TextBox Grid.Row="2" Grid.Column="1" IsEnabled="False" Text="{Binding Record.SpeedRate1}" Margin="5"/>
|
<TextBox Grid.Row="2" Grid.Column="1" IsEnabled="False" Text="{Binding Record.SpeedRate1}" Margin="5"/>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
Background="#F5F7FA" >
|
Background="#F5F7FA" >
|
||||||
<UserControl.Resources>
|
<UserControl.Resources>
|
||||||
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
|
<conv:InverseBooleanConverter x:Key="InverseBooleanConverter"/>
|
||||||
|
<conv:DoubleStringConverter x:Key="DoubleStringConverter"/>
|
||||||
|
|
||||||
<!-- 统一按钮样式 -->
|
<!-- 统一按钮样式 -->
|
||||||
<Style TargetType="Button">
|
<Style TargetType="Button">
|
||||||
@@ -137,7 +138,7 @@
|
|||||||
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Record.RoomTemperature}"/>-->
|
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Record.RoomTemperature}"/>-->
|
||||||
|
|
||||||
<Label Grid.Row="1" Grid.Column="2" Content="浸润时间(h):"/>
|
<Label Grid.Row="1" Grid.Column="2" Content="浸润时间(h):"/>
|
||||||
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding Record.SoakingTime}"/>
|
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding Record.SoakingTime, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleStringConverter}}"/>
|
||||||
|
|
||||||
<Label Grid.Row="1" Grid.Column="0" Content="测试液体:"/>
|
<Label Grid.Row="1" Grid.Column="0" Content="测试液体:"/>
|
||||||
<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Liquids}"
|
<ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Liquids}"
|
||||||
@@ -178,9 +179,14 @@
|
|||||||
<TextBlock Text="L/min" VerticalAlignment="Center" Margin="5,0,0,0"/>
|
<TextBlock Text="L/min" VerticalAlignment="Center" Margin="5,0,0,0"/>
|
||||||
</StackPanel>-->
|
</StackPanel>-->
|
||||||
|
|
||||||
|
<Label Grid.Row="4" Grid.Column="0" Content="加压速率:"/>
|
||||||
|
<TextBox Grid.Row="4" Grid.Column="1" IsEnabled="False" Text="{Binding SpeedRate1, StringFormat=F2}" Margin="5"/>
|
||||||
|
|
||||||
<Label Grid.Row="4" Grid.Column="0" Content="实时流量(L/min):"/>
|
|
||||||
<TextBox Grid.Row="4" Grid.Column="1" IsEnabled="False" Text="{Binding CurrentFlow, StringFormat=F2}"/>
|
<Label Grid.Row="4" Grid.Column="2" Content="实时流量(L/min):"/>
|
||||||
|
<TextBox Grid.Row="4" Grid.Column="3" IsEnabled="False" Text="{Binding CurrentFlow, StringFormat=F2}"/>
|
||||||
|
<!--<Label Grid.Row="4" Grid.Column="0" Content="实时流量(L/min):"/>
|
||||||
|
<TextBox Grid.Row="4" Grid.Column="1" IsEnabled="False" Text="{Binding CurrentFlow, StringFormat=F2}"/>-->
|
||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</GroupBox>
|
</GroupBox>
|
||||||
|
|||||||
Reference in New Issue
Block a user