187 lines
6.8 KiB
C#
187 lines
6.8 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using Telerik.Windows.Controls;
|
|
using XP.Hardware.RaySource.ViewModels;
|
|
|
|
namespace XP.Hardware.RaySource.Views
|
|
{
|
|
/// <summary>
|
|
/// 射线源操作视图 code-behind | X-Ray Source Operate View code-behind
|
|
/// 负责将滑块 DragCompleted / 输入框确认操作桥接到 ViewModel 命令
|
|
/// Bridges slider DragCompleted / numeric confirm actions to ViewModel commands
|
|
/// </summary>
|
|
public partial class RaySourceOperateView : UserControl
|
|
{
|
|
// 防止初始化阶段触发写入 | Prevent writes during initialization
|
|
private bool _isLoaded = false;
|
|
|
|
// 标记输入框是否正在键盘输入中(区分手动输入和箭头点击)
|
|
// Flag whether numeric input is in keyboard editing (distinguish manual input from arrow click)
|
|
private bool _isVoltageEditing = false;
|
|
private bool _isCurrentEditing = false;
|
|
|
|
public RaySourceOperateView()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// 滑块拖动完成时提交值到硬件 | Apply value to hardware when slider drag completes
|
|
VoltageSlider.DragCompleted += VoltageSlider_DragCompleted;
|
|
CurrentSlider.DragCompleted += CurrentSlider_DragCompleted;
|
|
|
|
// 输入框事件 | Numeric input events
|
|
VoltageNumeric.GotKeyboardFocus += (s, e) => _isVoltageEditing = true;
|
|
VoltageNumeric.LostFocus += VoltageNumeric_LostFocus;
|
|
VoltageNumeric.KeyDown += VoltageNumeric_KeyDown;
|
|
VoltageNumeric.ValueChanged += VoltageNumeric_ValueChanged;
|
|
|
|
CurrentNumeric.GotKeyboardFocus += (s, e) => _isCurrentEditing = true;
|
|
CurrentNumeric.LostFocus += CurrentNumeric_LostFocus;
|
|
CurrentNumeric.KeyDown += CurrentNumeric_KeyDown;
|
|
CurrentNumeric.ValueChanged += CurrentNumeric_ValueChanged;
|
|
|
|
Loaded += OnViewLoaded;
|
|
Unloaded += OnViewUnloaded;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 界面加载完成:标记已加载并触发自动初始化流程
|
|
/// View loaded: mark as loaded and trigger auto-initialization sequence
|
|
/// 参考旧代码 AreaDetector_Load 模式 | Follows AreaDetector_Load pattern from legacy code
|
|
/// </summary>
|
|
private async void OnViewLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
_isLoaded = true;
|
|
|
|
if (DataContext is RaySourceOperateViewModel vm)
|
|
{
|
|
await vm.AutoInitializeAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 界面卸载:触发 ViewModel 的 Dispose 执行断开操作
|
|
/// View unloaded: trigger ViewModel Dispose to execute disconnect
|
|
/// </summary>
|
|
private void OnViewUnloaded(object sender, RoutedEventArgs e)
|
|
{
|
|
if (DataContext is RaySourceOperateViewModel vm)
|
|
{
|
|
vm.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断是否可以提交(已加载、非设备反馈更新中)| Check if can apply (loaded and not updating from device)
|
|
/// </summary>
|
|
private bool CanApply(out RaySourceOperateViewModel vm)
|
|
{
|
|
vm = null;
|
|
if (!_isLoaded) return false;
|
|
if (DataContext is not RaySourceOperateViewModel viewModel) return false;
|
|
if (viewModel.IsUpdatingFromDevice) return false;
|
|
vm = viewModel;
|
|
return true;
|
|
}
|
|
|
|
#region 滑块事件 | Slider Events
|
|
|
|
/// <summary>
|
|
/// 电压滑块拖动完成 | Voltage slider drag completed
|
|
/// </summary>
|
|
private void VoltageSlider_DragCompleted(object sender, RadDragCompletedEventArgs e)
|
|
{
|
|
if (CanApply(out var vm))
|
|
vm.ApplyVoltageCommand.Execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电流滑块拖动完成 | Current slider drag completed
|
|
/// </summary>
|
|
private void CurrentSlider_DragCompleted(object sender, RadDragCompletedEventArgs e)
|
|
{
|
|
if (CanApply(out var vm))
|
|
vm.ApplyCurrentCommand.Execute();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 电压输入框事件 | Voltage Numeric Events
|
|
|
|
/// <summary>
|
|
/// 电压输入框值变更:仅在非键盘编辑时提交(即箭头按钮点击)
|
|
/// Voltage numeric value changed: only apply when not keyboard editing (i.e. arrow button click)
|
|
/// </summary>
|
|
private void VoltageNumeric_ValueChanged(object sender, RadRangeBaseValueChangedEventArgs e)
|
|
{
|
|
if (!_isVoltageEditing && CanApply(out var vm))
|
|
vm.ApplyVoltageCommand.Execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电压输入框回车确认 | Voltage numeric Enter key confirm
|
|
/// </summary>
|
|
private void VoltageNumeric_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter && CanApply(out var vm))
|
|
{
|
|
_isVoltageEditing = false;
|
|
vm.ApplyVoltageCommand.Execute();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电压输入框失焦确认 | Voltage numeric lost focus confirm
|
|
/// </summary>
|
|
private void VoltageNumeric_LostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
if (_isVoltageEditing && CanApply(out var vm))
|
|
{
|
|
_isVoltageEditing = false;
|
|
vm.ApplyVoltageCommand.Execute();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 电流输入框事件 | Current Numeric Events
|
|
|
|
/// <summary>
|
|
/// 电流输入框值变更:仅在非键盘编辑时提交(即箭头按钮点击)
|
|
/// Current numeric value changed: only apply when not keyboard editing (i.e. arrow button click)
|
|
/// </summary>
|
|
private void CurrentNumeric_ValueChanged(object sender, RadRangeBaseValueChangedEventArgs e)
|
|
{
|
|
if (!_isCurrentEditing && CanApply(out var vm))
|
|
vm.ApplyCurrentCommand.Execute();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电流输入框回车确认 | Current numeric Enter key confirm
|
|
/// </summary>
|
|
private void CurrentNumeric_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key == Key.Enter && CanApply(out var vm))
|
|
{
|
|
_isCurrentEditing = false;
|
|
vm.ApplyCurrentCommand.Execute();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 电流输入框失焦确认 | Current numeric lost focus confirm
|
|
/// </summary>
|
|
private void CurrentNumeric_LostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
if (_isCurrentEditing && CanApply(out var vm))
|
|
{
|
|
_isCurrentEditing = false;
|
|
vm.ApplyCurrentCommand.Execute();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|