!提交代码
This commit is contained in:
219
货架标准上位机/ViewModels/SetViewModel.cs
Normal file
219
货架标准上位机/ViewModels/SetViewModel.cs
Normal file
@ -0,0 +1,219 @@
|
||||
using HandyControl.Controls;
|
||||
using Microsoft.Win32;
|
||||
using Ping9719.WpfEx.Mvvm;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace 货架标准上位机.ViewModel
|
||||
{
|
||||
public class SetViewModel : BindableBase
|
||||
{
|
||||
private List<int> SaveLoginCountData_ = new List<int> { 3, 5, 7, 10 };
|
||||
//记忆最大数量下拉框
|
||||
public List<int> SaveLoginCountData { get => SaveLoginCountData_; set { SetProperty(ref SaveLoginCountData_, value); } }
|
||||
|
||||
private Dictionary<int, string> LogTimeData_ = new Dictionary<int, string>()
|
||||
{
|
||||
{ 30,"一月"},{ 91,"三月"},{ 182,"半年"},{ 365,"一年"},{ 1095,"三年"},{ -1,"永久"},
|
||||
};
|
||||
//日志缓存时间下拉框
|
||||
public Dictionary<int, string> LogTimeData { get => LogTimeData_; set { SetProperty(ref LogTimeData_, value); } }
|
||||
|
||||
|
||||
#region 页面输入信息
|
||||
private List<string> scannerComList;
|
||||
public List<string> ScannerComList
|
||||
{
|
||||
get { return scannerComList; }
|
||||
set
|
||||
{
|
||||
SetProperty(ref scannerComList, value);
|
||||
}
|
||||
}
|
||||
|
||||
private string selectedScannerCom;
|
||||
public string SelectedScannerCom
|
||||
{
|
||||
get { return selectedScannerCom; }
|
||||
set
|
||||
{
|
||||
SetProperty(ref selectedScannerCom, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<string> comList;
|
||||
public List<string> COMList
|
||||
{
|
||||
get { return comList; }
|
||||
set
|
||||
{
|
||||
SetProperty(ref comList, value);
|
||||
}
|
||||
}
|
||||
|
||||
private string selectedCOM;
|
||||
public string SelectedCOM
|
||||
{
|
||||
get { return selectedCOM; }
|
||||
set
|
||||
{
|
||||
SetProperty(ref selectedCOM, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private bool Powerboot_;
|
||||
public bool Powerboot { get => Powerboot_; set { SetProperty(ref Powerboot_, value); } }
|
||||
|
||||
private bool StartupFull_;
|
||||
public bool StartupFull { get => StartupFull_; set { SetProperty(ref StartupFull_, value); } }
|
||||
|
||||
private bool IsSaveLogin_;
|
||||
public bool IsSaveLogin { get => IsSaveLogin_; set { SetProperty(ref IsSaveLogin_, value); } }
|
||||
|
||||
private int SaveLoginCount_;
|
||||
public int SaveLoginCount { get => SaveLoginCount_; set { SetProperty(ref SaveLoginCount_, value); } }
|
||||
|
||||
private int SaveLogDay_;
|
||||
public int SaveLogDay { get => SaveLogDay_; set { SetProperty(ref SaveLogDay_, value); } }
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 刷新界面
|
||||
/// </summary>
|
||||
public void Update()
|
||||
{
|
||||
#region 加载配置项
|
||||
try
|
||||
{
|
||||
LocalFile.UpdateConfig();
|
||||
ScannerComList = LocalFile.Config.ScannerComList;
|
||||
Powerboot = LocalFile.Config.Sys.Powerboot;
|
||||
StartupFull = LocalFile.Config.Sys.StartupFull;
|
||||
IsSaveLogin = LocalFile.Config.Sys.IsSaveLogin;
|
||||
SaveLoginCount = LocalFile.Config.Sys.SaveLoginCount;
|
||||
SaveLogDay = LocalFile.Config.Sys.SaveLogDay;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Growl.Info($"获取配置失败。{ex.Message}");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 加载串口列表
|
||||
RefreshCOMList();
|
||||
#endregion
|
||||
}
|
||||
|
||||
public ICommand SaveCommand { get => new DelegateCommand(Save); }
|
||||
/// <summary>
|
||||
/// 保存
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
LocalFile.Config.Sys.Powerboot = Powerboot;
|
||||
LocalFile.Config.Sys.StartupFull = StartupFull;
|
||||
LocalFile.Config.Sys.IsSaveLogin = IsSaveLogin;
|
||||
LocalFile.Config.Sys.SaveLoginCount = SaveLoginCount;
|
||||
LocalFile.Config.Sys.SaveLogDay = SaveLogDay;
|
||||
LocalFile.Config.ScannerComList = ScannerComList;
|
||||
LocalFile.SaveConfig();
|
||||
Growl.Success($"保存成功。");
|
||||
|
||||
RunPowerboot(Powerboot);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LocalFile.UpdateConfig();
|
||||
Growl.Error($"保存失败。{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void RunPowerboot(bool isPowerboot)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (isPowerboot)
|
||||
{
|
||||
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
||||
if (rgkRun == null)
|
||||
rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
|
||||
|
||||
rgkRun.SetValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), "\"" + Path.Combine(LocalFile.AppDir, LocalFile.AppName) + "\"");
|
||||
}
|
||||
else
|
||||
{
|
||||
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
||||
if (rgkRun == null)
|
||||
rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
|
||||
|
||||
rgkRun.DeleteValue(Path.GetFileNameWithoutExtension(LocalFile.AppName), false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.Write(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ICommand AddCOMCommand { get => new DelegateCommand(AddCOM); }
|
||||
public void AddCOM()
|
||||
{
|
||||
//是否已选择COM号
|
||||
if (SelectedCOM == null)
|
||||
{
|
||||
Growl.Warning("请选择串口!");
|
||||
return;
|
||||
}
|
||||
//列表中是否存在
|
||||
var isExsist = ScannerComList.Where(t => t == SelectedCOM).Any();
|
||||
if (isExsist)
|
||||
{
|
||||
Growl.Warning($"已存在扫码枪{SelectedCOM}!");
|
||||
return;
|
||||
}
|
||||
//添加
|
||||
ScannerComList.Add(SelectedCOM);
|
||||
ScannerComList = ScannerComList.ToList();
|
||||
}
|
||||
|
||||
public ICommand DeleteCOMCommand { get => new DelegateCommand(DeleteCOM); }
|
||||
public void DeleteCOM()
|
||||
{
|
||||
//是否已选择COM号
|
||||
if (SelectedScannerCom == null)
|
||||
{
|
||||
Growl.Warning("请在下方选择串口!");
|
||||
return;
|
||||
}
|
||||
|
||||
ScannerComList.RemoveAll(t => t == SelectedScannerCom);
|
||||
ScannerComList = ScannerComList.ToList();
|
||||
}
|
||||
|
||||
public ICommand RefreshCOMListCommand { get => new DelegateCommand(RefreshCOMList); }
|
||||
public void RefreshCOMList()
|
||||
{
|
||||
try
|
||||
{
|
||||
COMList = SerialPort.GetPortNames().ToList();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user