Files
hoodFieldOfView/头罩视野slove/头罩视野/Views/RecordPage.xaml.cs

110 lines
3.9 KiB
C#
Raw Normal View History

2026-04-18 18:14:12 +08:00
using Modbus.Device;
2026-04-21 10:19:14 +08:00
using OfficeOpenXml;
2026-04-18 18:14:12 +08:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using .Services.Data;
2026-04-21 10:19:14 +08:00
using static .TestDataStore;
2026-04-18 18:14:12 +08:00
namespace .Views
{
2026-04-21 10:19:14 +08:00
2026-04-18 18:14:12 +08:00
/// <summary>
/// RecordPage.xaml 的交互逻辑
/// </summary>
public partial class RecordPage : Page
{
2026-04-21 10:19:14 +08:00
2026-04-18 18:14:12 +08:00
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
public RecordPage()
{
InitializeComponent();
}
//#region 2. 清除表格数据
private void btnClear_Click(object sender, RoutedEventArgs e)
{
// 确认清除
if (MessageBox.Show("确定要清除所有记录吗?", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
2026-04-21 10:19:14 +08:00
TestDataStore.Records.Clear();
RecordDataGrid.ItemsSource = null;
RecordDataGrid.ItemsSource = TestDataStore.Records;
2026-04-18 18:14:12 +08:00
}
}
//#endregion
//#region 3. 保存为Excel
private void btnSave_Click(object sender, RoutedEventArgs e)
{
2026-04-21 10:19:14 +08:00
try
2026-04-18 18:14:12 +08:00
{
2026-04-21 10:19:14 +08:00
// 1. 构建表格内容
StringBuilder sb = new StringBuilder();
sb.AppendLine("编号,日期,时间,左目视野面积,右目视野面积,双目视野面积,空白视野面积,下方视野,视野保存率");
2026-04-18 18:14:12 +08:00
2026-04-21 10:19:14 +08:00
foreach (var item in TestDataStore.Records)
{
sb.AppendLine($"{item.Id},{item.Date},{item.Time},{item.LeftEyeArea},{item.RightEyeArea},{item.BinocularArea},{item.LowerVision},{item.VisionRetentionRate}");
}
2026-04-20 14:03:01 +08:00
2026-04-21 10:19:14 +08:00
// 2. 弹出保存框,让用户自己选位置
Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv|所有文件 (*.*)|*.*";
saveFileDialog.FileName = $"测试记录_{DateTime.Now:yyyyMMddHHmmss}";
2026-04-20 14:03:01 +08:00
2026-04-21 10:19:14 +08:00
if (saveFileDialog.ShowDialog() == true)
2026-04-18 18:14:12 +08:00
{
2026-04-21 10:19:14 +08:00
// 3. 保存文件
File.WriteAllText(saveFileDialog.FileName, sb.ToString(), Encoding.UTF8);
MessageBox.Show("保存成功!\n文件路径" + saveFileDialog.FileName, "成功");
2026-04-18 18:14:12 +08:00
}
}
2026-04-21 10:19:14 +08:00
catch (Exception ex)
{
MessageBox.Show("保存失败:" + ex.Message);
}
2026-04-18 18:14:12 +08:00
}
//#endregion
private void Page_Loaded(object sender, RoutedEventArgs e)
{
2026-04-21 13:36:09 +08:00
// 判断连接
if (!ModbusHelper.IsConnected)
{
MessageBox.Show("未连接");
return;
}
// 获取客户端
var client = ModbusHelper.TcpClient;
//进入页面是否要保留原来的数据????,
2026-04-21 10:19:14 +08:00
RecordDataGrid.ItemsSource = null;
RecordDataGrid.ItemsSource = TestDataStore.Records;
2026-04-18 18:14:12 +08:00
2026-04-21 13:36:09 +08:00
2026-04-18 18:14:12 +08:00
}
private void GoHome(object s, RoutedEventArgs e) => NavigationService.Content = null;
private void GoTest(object s, RoutedEventArgs e) => NavigationService.Content = new Views.PageTest();
private void GoRecord(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordDate();
private void GoView(object s, RoutedEventArgs e) => NavigationService.Content = new Views.RecordPage();
}
}