alielie
2025-03-17 c401a2f2fd1a509cdeb33e850bb5c2827074af48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using HMI.AlarmInfoManage.Entity;
using HMI.Core.Util;
using HMI.UI.DpUtil;
using HMI.UI.Util;
using log4net;
using Panuon.WPF.UI;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
 
namespace HMI.AlarmInfoManage
{
    /// <summary>
    /// ModalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmSettingModal : WindowX
    {
        public AlarmSettingModal(ILog log)
        {
            this.log = log;
            this.DataContext = this;
            InitializeComponent();
        }
        private ILog log;
        public string FilePath { get; private set; }
        public int DataIndex { get; private set; }
        public AlarmSetting Data { get; private set; }
        public ObservableCollection<AlarmSetting> Datasource { get; private set; }
 
        private bool _isProcessing;
 
        protected override void OnClosing(CancelEventArgs e)
        {
            //Press Continue Button
            if (DialogResult == true)
            {
                if (_isProcessing)
                {
                    e.Cancel = true;
                    return;
                }
                var okButton = WindowXModalDialog.GetOKButton(this);
                try
                {
                    _isProcessing = true;
                    ButtonHelper.SetIsPending(okButton, true);
                    if (!ValidateForm())
                    {
                        e.Cancel = true;
                        return;
                    }
 
                    SaveOrUpdateData();
                }
                catch (Exception ex)
                {
                    log.Error("AlarmSetting Form Submit Failed.", ex);
                    ToastHelper.Error(this, $"Error: {ex.Message}");
                    e.Cancel = true;
                    return;
                }
                finally
                {
                    _isProcessing = false;
                    ButtonHelper.SetIsPending(okButton, false);
                }
            }
            base.OnClosing(e);
        }
 
        public bool ValidateDBAddress(object value, out string err)
        {
            err = null;
            int dbAddress = Convert.ToInt32(value);
            if (Datasource.FirstOrDefault(e => e.DBAddress == dbAddress) != null)
            {
                err = this.lang("App.MustBeUnique", this.lang("Alm.DbAddress"));
                return false;
            }
            return true;
        }
 
        private bool ValidateForm()
        {
            bool flag = FormGroupHelper.ValidateForm(formPanel);
            if (flag)
            {
                Data.DBAddress = Convert.ToInt32(dbAddress.Value);
                Data.StartAddress = Convert.ToInt32(startAddress.Value);
                Data.Length = Convert.ToInt32(length.Value);
            }
            return flag;
        }
 
        public bool? Open(int index, AlarmSetting data,
            ObservableCollection<AlarmSetting> dataSource,
            string filePath)
        {
            FilePath = filePath;
            DataIndex = index;
            Data = data;
            Datasource = dataSource;
            dbAddress.Value = Data.DBAddress;
            startAddress.Value = Data.StartAddress;
            length.Value = Data.Length;
            return this.ShowDialog();
        }
 
        private void SaveOrUpdateData()
        {
            if (DataIndex == -1)
            {
                InsertData(Data);
            }
            else
            {
                UpdateData(DataIndex, Data);
            }
        }
 
        public void InsertData(AlarmSetting data)
        {
            Datasource.Add(data);
            XmlHelper.SerializeToXml(FilePath, Datasource);
        }
 
        public void UpdateData(int index, AlarmSetting data)
        {
            Datasource[index] = data;
            XmlHelper.SerializeToXml(FilePath, Datasource);
        }
    }
}