页面逻辑添加

This commit is contained in:
2026-05-15 14:35:12 +08:00
parent a050c307a8
commit 2f2d312095
3 changed files with 159 additions and 6 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

@@ -123,11 +123,11 @@
<!-- 按钮区 --> <!-- 按钮区 -->
<WrapPanel Grid.Row="3" HorizontalAlignment="Center" Margin="0,10"> <WrapPanel Grid.Row="3" HorizontalAlignment="Center" Margin="0,10">
<Button Command="{Binding HardnessUpCommand}" Content="梁杆上升" Style="{StaticResource ActionButton}" Background="#FF9800" Margin="5 10 5 10"/> <Button Command="{Binding HardnessUpCommand}" Content="梁杆上升" Style="{StaticResource ActionButton}" Background="#FF9800" Margin="5 10 5 10" Click="btnUp_Click"/>
<Button Command="{Binding HardnessDownCommand}" Content="梁杆下降" Style="{StaticResource ActionButton}" Background="#FF9800"/> <Button Command="{Binding HardnessDownCommand}" Content="梁杆下降" Style="{StaticResource ActionButton}" Background="#FF9800" Click="btnDown_Click"/>
<Button Command="{Binding HardnessResetCommand}" Content="复位" Style="{StaticResource ActionButton}" Background="#9E9E9E"/> <Button Command="{Binding HardnessResetCommand}" Content="复位" Style="{StaticResource ActionButton}" Background="#9E9E9E" Click="btnReset_Click"/>
<Button Command="{Binding PrintHardnessCommand}" Content="打印" Style="{StaticResource ActionButton}" Background="#607D8B"/> <Button Command="{Binding PrintHardnessCommand}" Content="打印" Style="{StaticResource ActionButton}" Background="#607D8B" Click="btnPrint_Click"/>
<Button Command="{Binding StartHardnessCommand}" Content="启动测试" Style="{StaticResource ActionButton}" Background="#4CAF50" Margin="5 0 0 0"/> <Button Command="{Binding StartHardnessCommand}" Content="启动测试" Style="{StaticResource ActionButton}" Background="#4CAF50" Margin="5 0 0 0" Click="btnStartTest_Click"/>
</WrapPanel> </WrapPanel>
</Grid> </Grid>
</TabItem> </TabItem>

View File

@@ -1,12 +1,57 @@
using System.Windows; using System.Windows;
using TabletTester2025.ViewModels; // 加上这一行
namespace TabletTester2025 namespace TabletTester2025
{ {
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
// 硬度测试的ViewModel实例
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
} }
//硬度测试
// 启动测试按钮点击事件
private async void btnStartTest_Click(object sender, RoutedEventArgs e)
{
}
// 模拟PLC读取硬度值实际项目替换为你的ReadFloatAsync调用
private async Task<float> ReadPlcHardnessAsync()
{
await Task.Delay(100); // 模拟通信延迟
// 这里可以直接用你之前写好的PLC读取方法
// return await _plc.ReadFloatAsync((ushort)你的硬度地址);
return new Random().Next(80, 120) + (float)new Random().NextDouble() * 10;
}
// 复位按钮点击事件
private void btnReset_Click(object sender, RoutedEventArgs e)
{
}
// 梁杆上升按钮(示例)
private void btnUp_Click(object sender, RoutedEventArgs e)
{
}
// 梁杆下降按钮(示例)
private void btnDown_Click(object sender, RoutedEventArgs e)
{
}
// 打印按钮(示例)
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
}
} }
} }