using UIShare.UIViewModel; using UIShare.PubEvent; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using UIShare.GlobalVariable; using static UIShare.UIViewModel.ParameterVM; using UIShare.ViewModelBase; using Prism.Ioc; using Prism.Navigation.Regions; namespace TestingModule.ViewModels.Dialogs { public class ParameterSettingViewModel : DialogViewModelBase { #region 属性 private string _title = "参数设置界面"; public string Title { get => _title; set => SetProperty(ref _title, value); } private Array _EnumValues; public Array EnumValues { get => _EnumValues; set => SetProperty(ref _EnumValues, value); } private ObservableCollection _types = [ typeof(string), typeof(bool), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(byte[]), typeof(short[]), typeof(ushort[]), typeof(int[]), typeof(long[]), typeof(float[]), typeof(double[]), typeof(object) ]; public ObservableCollection Types { get => _types; set => SetProperty(ref _types, value); } private ObservableCollection _categories = new ObservableCollection( Enum.GetNames(typeof(ParameterCategory)) ); public ObservableCollection Categories { get => _categories; set => SetProperty(ref _categories, value); } private string _Mode; public string Mode { get => _Mode; set => SetProperty(ref _Mode, value); } private ProgramVM _program; public ProgramVM Program { get => _program; set => SetProperty(ref _program, value); } private ParameterVM _Parameter; public ParameterVM Parameter { get => _Parameter; set => SetProperty(ref _Parameter, value); } #endregion public DialogCloseListener RequestClose{get;set;} private ScopedContext _ScopedContext; public ICommand CancelCommand { get; set; } public ICommand SaveCommand { get; set; } public ParameterSettingViewModel(IContainerProvider containerProvider) : base(containerProvider) { CancelCommand = new DelegateCommand(Cancel); SaveCommand = new DelegateCommand(Save); } private void Save() { if (Mode == "ADD") { Program.Parameters.Add(Parameter); _ScopedContext.SelectedParameter = Parameter; } else { var index = Program.Parameters .Select((x, i) => new { x, i }) .FirstOrDefault(p => p.x.ID == _ScopedContext.SelectedParameter.ID)?.i; if (index.HasValue) { Program.Parameters[index.Value] = Parameter; } } RequestClose.Invoke(ButtonResult.OK); } private void Cancel() { RequestClose.Invoke(ButtonResult.No); } #region Prism Dialog 规范 public override void OnDialogClosed() { _eventAggregator.GetEvent().Publish(false); } public override void OnDialogOpened(IDialogParameters parameters) { if (parameters.ContainsKey("ScopedContext")) _ScopedContext = parameters.GetValue("ScopedContext"); _eventAggregator.GetEvent().Publish(true); Program =_ScopedContext.Program; Mode = parameters.GetValue("Mode"); if (Mode == "ADD") { Parameter = new(); } else { Parameter = new ParameterVM(_ScopedContext.SelectedParameter); } } #endregion } }