using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.ComponentModel; using System.Windows.Input; using System.Windows.Documents; using System.Threading; using System.Diagnostics; using System.Collections.ObjectModel; using System.IO; using GummingCommon; using System.Reflection; using GummingEntity; using GummingBusiness; using GummingControl; namespace Gumming { public class UnitViewFormulaViewModule : ViewModelBase { #region base parameters public UnitViewFormulaView View { get; set; } private bool isLoading; public bool IsConfirm { get; set; } #endregion #region constructor public UnitViewFormulaViewModule(bool canCreateNewTargetField = true) { View = new UnitViewFormulaView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } public void ShowDialog() { IsConfirm = false; View.ShowDialog(); } private void InitializeCommands() { CancelCommand = new DelegateCommand(OnCancelCommand); SaveCommand = new DelegateCommand(OnSaveCommand); SelectFormulaTypeCommand = new DelegateCommand(OnSelectFormulaTypeCommand); } public override void InitializeParameters(object content = null) { WindowTitle = ""; FormulaTypes = new ObservableCollection(Global.FormulaSations); FormulaTypes.Add(new HardStationType() { StationName = "None" }); //页面初始化加载列表 SelectedFormulaType = FormulaTypes[0]; OnSelectFormulaTypeCommand(SelectedFormulaType); if (HardFormulas?.Count > 0) { SelectedFormula = HardFormulas[0]; } } ~UnitViewFormulaViewModule() { } #endregion #region Binding Properties public string WindowTitle { get; private set; } private ObservableCollection _HardFormulas; public ObservableCollection HardFormulas { get { return _HardFormulas; } set { _HardFormulas = value; OnPropertyChanged("HardFormulas"); } } private HardFormulaEntity _SelectedFormula; public HardFormulaEntity SelectedFormula { get { return _SelectedFormula; } set { _SelectedFormula = value; OnPropertyChanged("SelectedFormula"); } } private ObservableCollection _FormulaTypes; public ObservableCollection FormulaTypes { get { return _FormulaTypes; } set { _FormulaTypes = value; OnPropertyChanged("FormulaTypes"); } } private HardStationType _SelectedFormulaType; public HardStationType SelectedFormulaType { get { return _SelectedFormulaType; } set { _SelectedFormulaType = value; OnPropertyChanged("SelectedFormulaType"); } } #endregion #region Binding Commands private ICommand _CancelCommand; public ICommand CancelCommand { get { return _CancelCommand; } set { _CancelCommand = value; OnPropertyChanged("CancelCommand"); } } private ICommand _SaveCommand; public ICommand SaveCommand { get { return _SaveCommand; } set { _SaveCommand = value; OnPropertyChanged("SaveCommand"); } } private ICommand _SelectFormulaTypeCommand; public ICommand SelectFormulaTypeCommand { get { return _SelectFormulaTypeCommand; } set { _SelectFormulaTypeCommand = value; OnPropertyChanged("SelectFormulaTypeCommand"); } } #endregion #region Private Methods private void OnCancelCommand(object sender) { IsConfirm = false; View.DialogResult = true; } private void OnSaveCommand(object sender) { IsConfirm = true; View.DialogResult = true; } private void StartLoadTestItem(string stationCode) { try { isLoading = true; if (HardFormulas == null) { HardFormulas = new ObservableCollection(); } HardFormulas.Clear(); var record = HardFormulaDA.Load(new PagerEntity() { PageIndex = 1, Rows = 999, Conditions = " and StationCode='" + stationCode + "' " }); List logs = (List)record.rows; View.Dispatcher.Invoke((Action)(() => { foreach (var s in logs) { HardFormulas.Add(s); } if (HardFormulas?.Count > 0) { SelectedFormula = HardFormulas[0]; } OnPropertyChanged("HartFormula"); isLoading = false; })); } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } finally { CloseProgressView(); } } private void OnSelectFormulaTypeCommand(Object sender) { if (sender == null) return; SelectedFormulaType = (HardStationType)sender; if (SelectedFormulaType != null) { try { StartLoadTestItem(SelectedFormulaType.StationCode); } catch (Exception ex) { ShowErrorBox(ex.Message); } finally { CloseProgressView(); } } } #endregion } }