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, $"未知异常!{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 = "DB地址已存在";
|
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);
|
}
|
}
|
}
|