十字辅助线

This commit is contained in:
李伟
2026-04-24 08:31:21 +08:00
parent c2650e737b
commit 7a0731386e
8 changed files with 120 additions and 1 deletions
@@ -10,6 +10,7 @@ using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using XP.ImageProcessing.RoiControl.Controls;
using Prism.Ioc;
using XplorePlane.Events;
using XplorePlane.ViewModels;
@@ -65,10 +66,25 @@ namespace XplorePlane.Views
if (e is CanvasClickedEventArgs args)
OnCanvasClicked(args.Position);
};
// 直接订阅 Prism 事件,不依赖 DataContext 的 PropertyChanged
try
{
var ea = Prism.Ioc.ContainerLocator.Current?.Resolve<Prism.Events.IEventAggregator>();
ea?.GetEvent<ToggleCrosshairEvent>().Subscribe(() =>
{
_crosshairVisible = !_crosshairVisible;
ToggleCrosshairOnCanvas(_crosshairVisible);
}, Prism.Events.ThreadOption.UIThread);
}
catch { /* 设计时或容器未初始化时忽略 */ }
}
private bool _crosshairVisible;
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"[VP] DataContextChanged: old={e.OldValue?.GetType().Name}, new={e.NewValue?.GetType().Name}");
if (e.OldValue is INotifyPropertyChanged oldVm)
oldVm.PropertyChanged -= OnVmPropertyChanged;
if (e.NewValue is INotifyPropertyChanged newVm)
@@ -78,6 +94,7 @@ namespace XplorePlane.Views
private void OnVmPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (sender is not ViewportPanelViewModel vm) return;
System.Diagnostics.Debug.WriteLine($"[VP] PropertyChanged: {e.PropertyName}");
if (e.PropertyName == nameof(ViewportPanelViewModel.CurrentMeasurementMode))
{
if (vm.CurrentMeasurementMode != MeasurementToolMode.None)
@@ -86,6 +103,10 @@ namespace XplorePlane.Views
else
RemoveOverlay();
}
else if (e.PropertyName == nameof(ViewportPanelViewModel.ShowCrosshair))
{
// 十字线通过直接订阅 ToggleCrosshairEvent 处理
}
}
#region
@@ -324,6 +345,47 @@ namespace XplorePlane.Views
return null;
}
private void UpdateCrosshairLines()
{
// 十字线位置通过 XAML 绑定自动计算
}
private Line _crosshairH, _crosshairV;
private void ToggleCrosshairOnCanvas(bool show)
{
var mainCanvas = FindChildByName<Canvas>(RoiCanvas, "mainCanvas");
System.Diagnostics.Debug.WriteLine($"[VP] ToggleCrosshair show={show}, mainCanvas={mainCanvas != null}, W={RoiCanvas.CanvasWidth}, H={RoiCanvas.CanvasHeight}");
if (mainCanvas == null) return;
if (show)
{
double w = RoiCanvas.CanvasWidth;
double h = RoiCanvas.CanvasHeight;
if (w <= 0 || h <= 0) return;
_crosshairH = new Line
{
X1 = 0, Y1 = h / 2, X2 = w, Y2 = h / 2,
Stroke = Brushes.Red, StrokeThickness = 1, Opacity = 0.7,
IsHitTestVisible = false
};
_crosshairV = new Line
{
X1 = w / 2, Y1 = 0, X2 = w / 2, Y2 = h,
Stroke = Brushes.Red, StrokeThickness = 1, Opacity = 0.7,
IsHitTestVisible = false
};
mainCanvas.Children.Add(_crosshairH);
mainCanvas.Children.Add(_crosshairV);
}
else
{
if (_crosshairH != null) { mainCanvas.Children.Remove(_crosshairH); _crosshairH = null; }
if (_crosshairV != null) { mainCanvas.Children.Remove(_crosshairV); _crosshairV = null; }
}
}
#endregion
#region