using Plugin.TestResult_WPF.Constant; using Plugin.TestResult_WPF.Model; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Plugin.TestResult_WPF { /// /// SingleCell.xaml 的交互逻辑 /// public partial class SingleCell : UserControl, INotifyPropertyChanged { #region ... public event PropertyChangedEventHandler PropertyChanged; public virtual void RaisePropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public void RaiseAndSetIfchanged(ref T old, T @new, [CallerMemberName] string propertyName = null) { old = @new; if (propertyName != null) RaisePropertyChanged(propertyName); } #endregion private InfoWindow window; private int maxSize; public SingleCell() { DataContext = this; InitializeComponent(); } public SingleCell(string parentName, int index, int maxSize) { this.parentName = parentName; this.index = index; this.maxSize = maxSize; DataContext = this; InitializeComponent(); } private string _parentName; public string parentName { get { return _parentName; } set { RaiseAndSetIfchanged(ref _parentName, value); RaisePropertyChanged("title"); } } private int _index; public int index { get { return _index; } set { RaiseAndSetIfchanged(ref _index, value); RaisePropertyChanged("title"); } } public string title => $"{parentName}{index + 1}"; private string _status = CommonStatus.WAITING; public string tooltipContent { get { string dataStr = ""; if (infos != null && infos.Count > 0) { foreach (var info in infos) { dataStr += $"\r\nStep:{info.step},Result:{info.result}"; } } return $"{title}\r\n状态:{CommonStatus.GetStatusDescription(status)}{dataStr}"; } } public string status { get { return _status; } set => RaiseAndSetIfchanged(ref _status, value); } private ConcurrentQueue queue = new ConcurrentQueue(); private ObservableCollection _infos; public ObservableCollection infos { get => _infos; set { RaiseAndSetIfchanged(ref _infos, value); RaisePropertyChanged("tooltipContent"); } } private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (window == null) { window = new InfoWindow(title, infos); window.Closed += Window_Closed; window.Owner = Window.GetWindow(this); window.Show(); } else { window.Activate(); } } private void Window_Closed(object sender, EventArgs e) { this.window = null; } public void SetStatus(string status) { this.status = status; } public void SetInfos(List infos) { this.infos = new ObservableCollection(infos); if (window != null) window.UpdateSource(this.infos); } public void SetInfos(Info info) { if (queue.Count >= maxSize) { queue.TryDequeue(out var first); queue.Enqueue(info); this.infos = new ObservableCollection(queue); if (window != null) window.UpdateSource(this.infos); } else { queue.Enqueue(info); if (window != null) { window.Dispatcher.Invoke(() => { this.infos.Add(info); }); } else { Dispatcher.Invoke(() => { this.infos.Add(info); }); } return; } } public void ClearInfo() { queue = new ConcurrentQueue(); var list = new ObservableCollection(queue); this.infos = list; if (window != null) window.UpdateSource(this.infos); } } }