This commit is contained in:
xyy
2026-05-15 15:50:45 +08:00
2 changed files with 140 additions and 13 deletions

108
ViewModels/YdTest.cs Normal file
View File

@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
namespace .ViewModels
{
internal class YdTest
{
// 测试参数(界面可设置)
private int _testCount = 8;
public int HardnessTestCount
{
get => _testCount;
set { _testCount = value; OnPropertyChanged(); }
}
private int _intervalSec = 9;
public int HardnessIntervalSec
{
get => _intervalSec;
set { _intervalSec = value; OnPropertyChanged(); }
}
// 测试结果(界面显示)
private float _max;
public float HardnessMax
{
get => _max;
set { _max = value; OnPropertyChanged(); }
}
private float _min;
public float HardnessMin
{
get => _min;
set { _min = value; OnPropertyChanged(); }
}
private float _avg;
public float HardnessAvg
{
get => _avg;
set { _avg = value; OnPropertyChanged(); }
}
// 状态显示
private int _completedCount;
public int HardnessCompletedCount
{
get => _completedCount;
set { _completedCount = value; OnPropertyChanged(); }
}
private float _currentValue;
public float HardnessCurrentValue
{
get => _currentValue;
set { _currentValue = value; OnPropertyChanged(); }
}
// 内部存储测试结果
private readonly List<float> _results = new List<float>();
// 添加单次结果并更新统计值
public void AddResult(float value)
{
_results.Add(value);
HardnessCurrentValue = value;
HardnessCompletedCount = _results.Count;
UpdateStats();
}
// 更新最大/最小/平均值
private void UpdateStats()
{
if (_results.Count == 0)
{
HardnessMax = 0;
HardnessMin = 0;
HardnessAvg = 0;
return;
}
HardnessMax = _results.Max();
HardnessMin = _results.Min();
HardnessAvg = (float)Math.Round(_results.Average(), 1);
}
// 重置所有数据
public void Reset()
{
_results.Clear();
HardnessMax = 0;
HardnessMin = 0;
HardnessAvg = 0;
HardnessCompletedCount = 0;
HardnessCurrentValue = 0;
}
// INotifyPropertyChanged 接口实现
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}

View File

@@ -6,35 +6,54 @@ namespace TabletTester2025
{ {
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
// 硬度测试的ViewModel实例
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
} }
//硬度测试
private void OnHardnessUpPress(object sender, MouseButtonEventArgs e) // 启动测试按钮点击事件
private async void btnStartTest_Click(object sender, RoutedEventArgs e)
{ {
if (sender is Button btn && btn.DataContext is ViewModels.StationViewModel vm)
vm.StartHardnessUp();
} }
private void OnHardnessUpRelease(object sender, MouseButtonEventArgs e) // 模拟PLC读取硬度值实际项目替换为你的ReadFloatAsync调用
private async Task<float> ReadPlcHardnessAsync()
{ {
if (sender is Button btn && btn.DataContext is ViewModels.StationViewModel vm) await Task.Delay(100); // 模拟通信延迟
vm.StopHardnessUp(); // 这里可以直接用你之前写好的PLC读取方法
// return await _plc.ReadFloatAsync((ushort)你的硬度地址);
return new Random().Next(80, 120) + (float)new Random().NextDouble() * 10;
} }
private void OnHardnessDownPress(object sender, MouseButtonEventArgs e) // 复位按钮点击事件
private void btnReset_Click(object sender, RoutedEventArgs e)
{ {
if (sender is Button btn && btn.DataContext is ViewModels.StationViewModel vm)
vm.StartHardnessDown();
} }
private void OnHardnessDownRelease(object sender, MouseButtonEventArgs e) // 梁杆上升按钮(示例)
private void btnUp_Click(object sender, RoutedEventArgs e)
{ {
if (sender is Button btn && btn.DataContext is ViewModels.StationViewModel vm)
vm.StopHardnessDown(); }
}
// 梁杆下降按钮(示例)
private void btnDown_Click(object sender, RoutedEventArgs e)
{
}
// 打印按钮(示例)
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
}
} }
} }