AHAQ_QGNH/AnQing/ViewModel/SetUpViewModel.cs
2025-05-19 09:31:47 +08:00

158 lines
4.6 KiB
C#

using AnQing.Commons;
using AnQing.Model;
using AnQing.Models;
using RGD.DBUtility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml;
namespace AnQing.ViewModel
{
public class SetUpViewModel: INotifyPropertyChanged
{
private string _videoIP1 =Common.GetSetting("VideoIP1");
public string VideoIP1
{
get => _videoIP1;
set
{
if (_videoIP1 != value)
{
_videoIP1 = value;
OnPropertyChanged(nameof(VideoIP1));
}
}
}
private string _videoIP2 = GetSetting("VideoIP2");
public string VideoIP2
{
get => _videoIP2;
set
{
if (_videoIP2 != value)
{
_videoIP2 = value;
OnPropertyChanged(nameof(VideoIP2));
}
}
}
private string _imagePath = GetSetting("ImagePath");
private ObservableCollection<IMAGEINFO> _dgScanMain = new ObservableCollection<IMAGEINFO>();
public ObservableCollection<IMAGEINFO> DgScanMain
{
get => _dgScanMain;
set
{
if (_dgScanMain != value)
{
_dgScanMain = value;
OnPropertyChanged();
}
}
}
private IMAGEINFO _selectedItem;
public IMAGEINFO SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged(nameof(SelectedItem));
}
}
public SetUpViewModel()
{
LoadScanMain();
}
private void LoadScanMain()
{
Task.Run(() =>
{
DataView dv = DbHelperSQL.Query("select * from IMAGEINFO").Tables[0].DefaultView;
DgScanMain = Common.ConvertDataViewToObservableCollection<IMAGEINFO>(dv);
//DgScanMain.Add(new IMAGEINFO() { x = 0, y = 0, width = 0, height = 0, index = 0 });
});
}
public string ImagePath
{
get => _imagePath;
set
{
if (_imagePath != value)
{
_imagePath = value;
OnPropertyChanged(nameof(ImagePath));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ICommand buttonQuitCommand;
public ICommand ButtonQuitCommand => buttonQuitCommand ??= new RelayCommand(ButtonSave);
/// <summary>
/// 退出页面
/// </summary>
private void ButtonSave()
{
try
{
SetSetting("VideoIP1", VideoIP1);
SetSetting("VideoIP2", VideoIP2);
SetSetting("ImagePath", ImagePath);
App.Restart();
}
catch (Exception ex)
{
// 处理异常
}
}
public static void SetSetting(string key, string value)
{
string configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFile);
XmlNode appSettingsNode = xmlDoc.SelectSingleNode("//appSettings");
if (appSettingsNode != null)
{
XmlNode settingNode = appSettingsNode.SelectSingleNode($"add[@key='{key}']");
if (settingNode != null)
{
settingNode.Attributes["value"].Value = value;
}
else
{
XmlElement newSetting = xmlDoc.CreateElement("add");
newSetting.SetAttribute("key", key);
newSetting.SetAttribute("value", value);
appSettingsNode.AppendChild(newSetting);
}
xmlDoc.Save(configFile);
}
}
public static string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
}
}