Files
XplorePlane/XP.Hardware.Detector/Views/DetectorConfigView.xaml.cs
T

83 lines
2.9 KiB
C#

using System;
using System.Windows;
using System.Windows.Controls;
using Prism.Events;
using Prism.Ioc;
using XP.Hardware.Detector.Abstractions;
using XP.Hardware.Detector.Abstractions.Events;
namespace XP.Hardware.Detector.Views
{
/// <summary>
/// 面阵探测器配置视图 | Area detector configuration view
/// 订阅探测器图像采集事件,将图像数据传递给直方图控件
/// </summary>
public partial class DetectorConfigView : UserControl
{
private IEventAggregator _eventAggregator;
private SubscriptionToken _imageSubscriptionToken;
public DetectorConfigView()
{
InitializeComponent();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
/// <summary>
/// 加载时订阅图像采集事件 | Subscribe to image captured event on load
/// </summary>
private void OnLoaded(object sender, RoutedEventArgs e)
{
try
{
_eventAggregator = ContainerLocator.Current?.Resolve<IEventAggregator>();
if (_eventAggregator != null)
{
_imageSubscriptionToken = _eventAggregator.GetEvent<ImageCapturedEvent>()
.Subscribe(OnImageCaptured, ThreadOption.BackgroundThread);
}
}
catch
{
// 事件聚合器不可用时静默降级 | Silent degradation when event aggregator unavailable
}
}
/// <summary>
/// 卸载时取消订阅 | Unsubscribe on unload
/// </summary>
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_eventAggregator != null && _imageSubscriptionToken != null)
{
_eventAggregator.GetEvent<ImageCapturedEvent>().Unsubscribe(_imageSubscriptionToken);
_imageSubscriptionToken = null;
}
}
/// <summary>
/// 图像采集回调:将 ushort[] 转为 byte[] 后传给直方图控件 | Image captured callback
/// </summary>
private void OnImageCaptured(ImageCapturedEventArgs args)
{
if (args?.ImageData == null || args.Width == 0 || args.Height == 0)
return;
try
{
// 将 ushort[] 转换为 little-endian byte[] | Convert ushort[] to little-endian byte[]
var rawBytes = new byte[args.ImageData.Length * 2];
Buffer.BlockCopy(args.ImageData, 0, rawBytes, 0, rawBytes.Length);
// 调用直方图控件更新(控件内部支持从非 UI 线程调用)| Update histogram control (supports non-UI thread calls)
HistogramControl?.UpdateImage(rawBytes, (int)args.Width, (int)args.Height, 16);
}
catch
{
// 异常不影响主流程 | Exception does not affect main flow
}
}
}
}