using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using Aitex.UI.Charting.Model; using Aitex.UI.Charting.ViewModel; using System.Windows; using DataAnalysisControl.Core; namespace Aitex.UI.Charting.Command { public class AddDataSourceCommand : ICommand { public AddDataSourceCommand(IDataSource dataSource) { _dataSource = dataSource; } IDataSource _dataSource; public bool CanExecute(object parameter) { return true; } #pragma warning disable 0067 public event EventHandler CanExecuteChanged; #pragma warning restore 0067 private void AsyncShowWarningMessageBox(string message, string title) { Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning))); } public void Execute(object parameter) { string reason = string.Empty; try { var commonViewModel = (CommonViewModel)parameter; if (commonViewModel.DataSources.ContainsKey(_dataSource.Title)) { if (_dataSource.BeginTime == commonViewModel.DataSources[_dataSource.Title].BeginTime && _dataSource.EndTime == commonViewModel.DataSources[_dataSource.Title].EndTime) { reason = string.Format("数据名 \"{0}\" 已存在,未添加到数据源列表中...\n", _dataSource.Title); CONTEXT.WriteLog(reason); AsyncShowWarningMessageBox(reason, "警告"); return; } else { //Recipe name相同,炉次不同,默认在数据源后补上1,2,3,4后缀以示区别 for (int postNum = 1; postNum < 100; postNum++) { string newDsName = string.Format("{0}({1})", _dataSource.Title, postNum); if (!commonViewModel.DataSources.ContainsKey(newDsName)) { _dataSource.Title = newDsName; break; } } } } commonViewModel.DataSources.Add(_dataSource.Title, _dataSource); commonViewModel.CurrentSelectedDataSource = _dataSource; commonViewModel.InvokePropertyChanged(); } catch (Exception ex) { CONTEXT.WriteLog(ex); AsyncShowWarningMessageBox(string.Format("添加数据源{0}发生错误:\n{1}", _dataSource.Title, ex.Message), "警告"); } } } }