using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using NavisworksTransport.UI.WPF.ViewModels;
namespace NavisworksTransport.UI.WPF.Views
{
///
/// PathAnalysisDialog.xaml 的交互逻辑
/// 路径规划分析对话框 - 支持多维度评分和同终点组分析(非模态窗口)
///
public partial class PathAnalysisDialog : Window
{
#region 单例模式
private static PathAnalysisDialog _currentInstance = null;
private static readonly object _lock = new object();
#endregion
public PathAnalysisDialog()
{
try
{
InitializeComponent();
DataContext = new ViewModels.PathAnalysisViewModel();
LogManager.Info("PathAnalysisDialog初始化完成");
Title = $"路径规划分析 - 多路径对比 [{DateTime.Now:MM-dd HH:mm}]";
}
catch (Exception ex)
{
LogManager.Error($"初始化PathAnalysisDialog失败: {ex.Message}", ex);
}
}
///
/// 显示路径分析窗口(单例模式,非模态)
///
public static PathAnalysisDialog ShowAnalysis(Window owner = null)
{
try
{
lock (_lock)
{
// 如果当前已有实例且仍然打开,则激活窗口
if (_currentInstance != null)
{
try
{
if (_currentInstance.IsLoaded)
{
_currentInstance.Activate();
_currentInstance.Focus();
LogManager.Info("PathAnalysisDialog已激活现有窗口");
return _currentInstance;
}
}
catch (Exception)
{
// 如果访问失败,认为窗口已关闭,创建新实例
_currentInstance = null;
}
}
// 创建新实例
var dialog = new PathAnalysisDialog();
if (owner != null)
{
dialog.Owner = owner;
}
// 设置为当前实例
_currentInstance = dialog;
// 使用Show()非模态显示,允许与主窗口同时操作
dialog.Show();
LogManager.Info("PathAnalysisDialog已以非模态方式显示");
return dialog;
}
}
catch (Exception ex)
{
LogManager.Error($"显示路径分析窗口失败: {ex.Message}");
return null;
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
try
{
LogManager.Info("用户关闭路径分析对话框");
this.Close();
}
catch (Exception ex)
{
LogManager.Error($"关闭PathAnalysisDialog时发生错误: {ex.Message}", ex);
}
}
protected override void OnClosed(EventArgs e)
{
try
{
LogManager.Info("PathAnalysisDialog已关闭");
// 清理单例引用
lock (_lock)
{
if (_currentInstance == this)
{
_currentInstance = null;
}
}
base.OnClosed(e);
}
catch (Exception ex)
{
LogManager.Error($"PathAnalysisDialog清理过程中发生错误: {ex.Message}", ex);
}
}
}
#region 值转换器
///
/// 分数转颜色转换器
///
public class ScoreToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double score)
{
if (score >= 80) return new SolidColorBrush(Colors.Green);
if (score >= 60) return new SolidColorBrush(Colors.Orange);
return new SolidColorBrush(Colors.Red);
}
return new SolidColorBrush(Colors.Gray);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
///
/// 优先级转颜色转换器
///
public class PriorityToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int priority)
{
if (priority >= 4) return new SolidColorBrush(Color.FromRgb(220, 53, 69)); // 红色 - 高优先级
if (priority >= 3) return new SolidColorBrush(Color.FromRgb(255, 193, 7)); // 黄色 - 中优先级
return new SolidColorBrush(Color.FromRgb(40, 167, 69)); // 绿色 - 低优先级
}
return new SolidColorBrush(Colors.Gray);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
///
/// 策略描述转换器
///
public class StrategyDescriptionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string strategy)
{
switch (strategy)
{
case "安全优先":
return "优先选择无碰撞路径,适合精密组件运输(安全权重50%)";
case "效率优先":
return "优先选择最短路径,适合紧急任务(效率权重40%)";
case "平衡模式":
return "综合考虑安全与效率,通用场景推荐(默认)";
default:
return "请选择分析策略";
}
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
///
/// 布尔转背景色转换器
///
public class BoolToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool isSelected && isSelected)
{
return new SolidColorBrush(Color.FromRgb(230, 240, 255)); // 选中时的浅蓝色
}
return new SolidColorBrush(Colors.Transparent);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
///
/// Null转Visibility转换器
///
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
#endregion
}