using CommunityToolkit.Mvvm.Input; using FLY.Thick.Base.IService; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using Unity; namespace FLY.Thick.Base.UI { /// /// Page_GetSample.xaml 的交互逻辑 /// public partial class PgGetSample : Page { IGetSampleService getSampleService; IInitParamService initParamService; PgGetSampleVm viewModel; public PgGetSample() { InitializeComponent(); if (System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Runtime) return; this.Loaded += UcGridGraph_Loaded; this.Unloaded += UcGridGraph_Unloaded; } private void UcGridGraph_Unloaded(object sender, RoutedEventArgs e) { viewModel.DisposeBinding(); } private void UcGridGraph_Loaded(object sender, RoutedEventArgs e) { viewModel.SetBinding(); } [InjectionMethod] public void Init( IGetSampleService getSampleService, IInitParamService initParamService, string infoName = null) { this.getSampleService = getSampleService; this.initParamService = initParamService; viewModel = new PgGetSampleVm(); viewModel.Init(getSampleService, infoName); this.DataContext = viewModel; this.grid_initparam.DataContext = this.initParamService; } } public class PgGetSampleVm : INotifyPropertyChanged { #region Markno const int MARKNO_UPDATE_SAMPLE = 1; const int MARKNO_UPDATE_FEATURE = 2; #endregion #region 参数 /// /// 参数:使能 /// public bool Enable { get; set; } /// /// 参数:样品点范围 /// public int SampleRange { get; set; } /// /// 参数:移动滤波 ,窗口值 /// public int Window { get; set; } /// /// 使用%方式检查异常 /// public bool IsCheckByPercent { get; set; } /// /// 异常% /// public double ErrPercent { get; set; } /// /// 异常值 /// public int ErrValue { get; set; } /// /// 当前 样品X_AD/样品0_AD 与 上一次比较 差异比例 单位不是% /// public double CrossErrPercent { get; set; } /// /// 参数:样品点参数 /// public SampleCellView[] Samples { get; set; } #endregion #region Command public RelayCommand ApplyCmd { get; private set; } #endregion public event PropertyChangedEventHandler PropertyChanged; public string InfoName { get; set; } IGetSampleService getSampleService; Dictionary> bindingConexts = new Dictionary>(); /// /// 数据已经绑定了 /// bool isBinding; public PgGetSampleVm() { ApplyCmd = new RelayCommand(Apply); } public void Init( IGetSampleService getSampleService, string infoName) { InfoName = infoName; this.getSampleService = getSampleService; SetBinding(); } /// /// 参数绑定 /// public void SetBinding() { if (isBinding) return; isBinding = true; //下面全部event保存在bindingConexts Misc.BindingOperations.StartMarkdownEvents(bindingConexts); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Enable), this, nameof(Enable)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.SampleRange), this, nameof(SampleRange)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Window), this, nameof(Window)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.IsCheckByPercent), this, nameof(IsCheckByPercent)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrPercent), this, nameof(ErrPercent)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrValue), this, nameof(ErrValue)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.CrossErrPercent), this, nameof(CrossErrPercent)); Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Samples), this,() => { FObjBase.PollModule.Current.Poll_JustOnce(updateSamples, this, MARKNO_UPDATE_SAMPLE); }); //备份event完毕, 必须关闭记录 Misc.BindingOperations.StopMarkdownEvents(); } /// /// 解除绑定 /// public void DisposeBinding() { if (!isBinding)//已经解除绑定了 return; isBinding = false; //释放全部 event Misc.BindingOperations.DisposeEventTargetObject(bindingConexts); } void updateSamples() { var samples = this.getSampleService.Samples; if (samples == null) { Samples = null; } else { var sampleViews = new SampleCellView[samples.Count()]; for (int i = 0; i < sampleViews.Count(); i++) { sampleViews[i] = new SampleCellView() { Name = i.ToString() }; var sampleView = sampleViews[i]; var sample = samples[i]; Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Enable), sampleView, nameof(SampleCellView.Enable)); Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.JustForCheck), sampleView, nameof(SampleCellView.JustForCheck)); Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.OrgAD), sampleView, nameof(SampleCellView.OrgAD)); Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Position), sampleView, nameof(SampleCellView.Position)); Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.AD), sampleView, nameof(SampleCellView.AD)); Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.SampleValue), sampleView, nameof(SampleCellView.SampleValue)); } updateSampleCellViewVisible(sampleViews); Samples = sampleViews; for (int i = 0; i < Samples.Count(); i++) { var sampleView = Samples[i]; sampleView.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(SampleCellView.Enable)) { updateSampleCellViewVisible(); } }; } } } protected void updateSampleCellViewVisible(SampleCellView[] sampleViews) { if (sampleViews == null) return; for (int i = 0; i < sampleViews.Count(); i++) { if (i == 0) sampleViews[i].IsVisible = true; else { sampleViews[i].IsVisible = sampleViews[i - 1].Enable; } } } protected void updateSampleCellViewVisible() { updateSampleCellViewVisible(Samples); } private void Apply() { if (!WdPassword.Authorize("GetSample")) return; this.getSampleService.Enable = this.Enable; this.getSampleService.SampleRange = this.SampleRange; this.getSampleService.Window = this.Window; this.getSampleService.IsCheckByPercent = this.IsCheckByPercent; this.getSampleService.ErrPercent = this.ErrPercent; this.getSampleService.ErrValue = this.ErrValue; this.getSampleService.CrossErrPercent = this.CrossErrPercent; for (int i = 0; i < Samples.Count(); i++) { var sample_src = this.Samples[i]; var sample_desp = this.getSampleService.Samples[i]; sample_desp.Enable = sample_src.Enable; sample_desp.JustForCheck = sample_src.JustForCheck; sample_desp.OrgAD = sample_src.OrgAD; sample_desp.Position = sample_src.Position; } this.getSampleService.SearchRange = 0; for (int i = 0; i < this.getSampleService.Features.Count(); i++) { var feature_desp = this.getSampleService.Features[i]; feature_desp.Enable = false; } getSampleService.Apply(); string tit = (string)Application.Current.TryFindResource("str.PgGetSample.ApplySuccessfully"); FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2)); } } public class PgGetSampleVmUt : PgGetSampleVm { public PgGetSampleVmUt() { Samples = new SampleCellView[3]; for (int i = 0; i < Samples.Count(); i++) { SampleCellView sampleCell = new SampleCellView() { Name = i.ToString() }; Samples[i] = sampleCell; } Enable = true; Samples[0].Enable = true; updateSampleCellViewVisible(); } } /// /// 样品点 /// public class SampleCellView : INotifyPropertyChanged { /// /// 序号 /// public string Name { get; set; } /// /// 显示出来 /// public bool IsVisible { get; set; } /// /// 参数:使能 /// public bool Enable { get; set; } /// /// 参数:只检查不标定 /// public bool JustForCheck { get; set; } /// /// 参数:原始AD值 /// public int OrgAD { get; set; } /// /// 参数:位置 /// public int Position { get; set; } public int AD { get; set; } public double SampleValue { get; set; } #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; #endregion } }