using System; using System.Diagnostics; using System.Windows; using Correlator.DataService; using Correlator.Model; using Correlator.Util; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; namespace Correlator.ViewModels { public class EditSoundSampleRateDialogViewModel : BindableBase, IDialogAware { public event Action<IDialogResult> RequestClose; #region VM public string Title { get; set; } private string _workMode; public string WorkMode { get => _workMode; set { _workMode = value; RaisePropertyChanged(); } } private string _sampleRate; public string SampleRate { get => _sampleRate; set { _sampleRate = value; RaisePropertyChanged(); } } #endregion #region DelegateCommand public DelegateCommand CancelCommand { get; set; } public DelegateCommand DetermineParamCommand { get; set; } #endregion private readonly IApplicationDataService _dataService; private SampleRateModel _selectedModel; public EditSoundSampleRateDialogViewModel(IApplicationDataService dataService) { _dataService = dataService; CancelCommand = new DelegateCommand(DismissDialog); DetermineParamCommand = new DelegateCommand(DetermineParam); } private void DismissDialog() { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } private void DetermineParam() { Debug.Assert(_selectedModel != null, nameof(_selectedModel) + " != null"); if (!_sampleRate.IsNumber()) { MessageBox.Show("请输入数字", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Stop); return; } Debug.Assert(_sampleRate != null, nameof(_sampleRate) + " != null"); _dataService.UpdateSampleRate(_selectedModel.WorkMode, int.Parse(_sampleRate)); RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { _selectedModel = parameters.GetValue<SampleRateModel>("SelectedSampleRateModel"); WorkMode = _selectedModel.WorkMode == 1 ? "加速度计" : "水听器"; SampleRate = _selectedModel.SampleRate.ToString(); Title = $"编辑采样率 - {WorkMode}"; } } }