Files
XplorePlane/XP.Hardware.PLC/Views/PlcTestBenchWindow.xaml.cs

63 lines
2.4 KiB
C#

using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using XP.Common.Logging.Interfaces;
using XP.Hardware.Plc.Abstractions;
using XP.Hardware.PLC.ViewModels;
namespace XP.Hardware.PLC.Views
{
/// <summary>
/// PLC 测试工具窗口 Code-Behind | PLC Test Bench Window Code-Behind
/// </summary>
public partial class PlcTestBenchWindow : Window
{
private readonly PlcTestBenchViewModel _viewModel;
/// <summary>
/// 构造函数 | Constructor
/// </summary>
/// <param name="plcClient">PLC 客户端接口 | PLC client interface</param>
/// <param name="logger">日志服务 | Logger service</param>
public PlcTestBenchWindow(IPlcClient plcClient, ILoggerService logger)
{
InitializeComponent();
_viewModel = new PlcTestBenchViewModel(plcClient, logger);
DataContext = _viewModel;
// 继承主窗口图标
if (Application.Current?.MainWindow != null)
{
Icon = Application.Current.MainWindow.Icon;
}
// 订阅日志集合变化,自动滚动到最新条目 | Subscribe to log collection changes for auto-scroll
_viewModel.LogEntries.CollectionChanged += LogEntries_CollectionChanged;
}
/// <summary>
/// 日志集合变化时自动滚动到最新条目 | Auto-scroll to latest entry on collection change
/// </summary>
private void LogEntries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && _viewModel.LogEntries.Count > 0)
{
// 使用 Dispatcher 延迟执行,确保 UI 渲染完成后再滚动 | Use Dispatcher to ensure UI rendering is complete before scrolling
Dispatcher.InvokeAsync(() =>
{
LogListBox.ScrollIntoView(_viewModel.LogEntries[_viewModel.LogEntries.Count - 1]);
}, System.Windows.Threading.DispatcherPriority.Background);
}
}
/// <summary>
/// 窗口关闭时释放资源 | Release resources on window closing
/// </summary>
protected override void OnClosing(CancelEventArgs e)
{
_viewModel?.Cleanup();
base.OnClosing(e);
}
}
}