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 _results = new List(); // 添加单次结果并更新统计值 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)); } } }