Files
hoodFieldOfView/头罩视野slove/头罩视野/Views/RecordPage.xaml.cs
2026-04-21 10:35:43 +08:00

100 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Modbus.Device;
using OfficeOpenXml;
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;
using static .TestDataStore;
namespace .Views
{
/// <summary>
/// RecordPage.xaml 的交互逻辑
/// </summary>
public partial class RecordPage : Page
{
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)
{
TestDataStore.Records.Clear();
RecordDataGrid.ItemsSource = null;
RecordDataGrid.ItemsSource = TestDataStore.Records;
}
}
//#endregion
//#region 3. 保存为Excel
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
// 1. 构建表格内容
StringBuilder sb = new StringBuilder();
sb.AppendLine("编号,日期,时间,左目视野面积,右目视野面积,双目视野面积,空白视野面积,下方视野,视野保存率");
foreach (var item in TestDataStore.Records)
{
sb.AppendLine($"{item.Id},{item.Date},{item.Time},{item.LeftEyeArea},{item.RightEyeArea},{item.BinocularArea},{item.LowerVision},{item.VisionRetentionRate}");
}
// 2. 弹出保存框,让用户自己选位置
Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv|所有文件 (*.*)|*.*";
saveFileDialog.FileName = $"测试记录_{DateTime.Now:yyyyMMddHHmmss}";
if (saveFileDialog.ShowDialog() == true)
{
// 3. 保存文件
File.WriteAllText(saveFileDialog.FileName, sb.ToString(), Encoding.UTF8);
MessageBox.Show("保存成功!\n文件路径" + saveFileDialog.FileName, "成功");
}
}
catch (Exception ex)
{
MessageBox.Show("保存失败:" + ex.Message);
}
}
//#endregion
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//进入页面是否要保留原来的数据????,
RecordDataGrid.ItemsSource = null;
RecordDataGrid.ItemsSource = TestDataStore.Records;
}
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();
}
}