XiangTan_JiaoJie_Bak/DM_Weight/ViewModels/SettingWindowViewModel.cs

90 lines
3.5 KiB
C#

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DM_Weight.select;
using System.Xml;
using log4net;
using log4net.Repository.Hierarchy;
namespace DM_Weight.ViewModels
{
public class SettingWindowViewModel : BindableBase, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(SettingWindowViewModel));
public bool KeepAlive => false;
public static List<OrderTakeSelect> defaultKeyValuePairs = new()
{
new OrderTakeSelect() { Code = "operator", Name = "操作人" },
new OrderTakeSelect() { Code = "reviewer", Name = "审核人" }
};
public List<OrderTakeSelect> keyValuePairs
{
get { return defaultKeyValuePairs; }
}
public static string _defaultLoginMode = string.Empty;// ConfigurationManager.AppSettings["loginMode"]??"1";
public static string _defaultFirstLogin = string.Empty;//ConfigurationManager.AppSettings["firstLogin"] ?? "operator";
private bool _loginMode = false;// _defaultLoginMode.Equals("2");
public bool LoginMode { get => _loginMode; set => SetProperty(ref _loginMode, value); }
private string _firstLogin = _defaultFirstLogin;
public string FirstLogin
{
get => _firstLogin; set => SetProperty(ref _firstLogin, value);
}
public SettingWindowViewModel()
{
logger.Info("进入SettingWindowViewModel");
FirstLogin = ReadAppSetting("firstLogin"); //ConfigurationManager.AppSettings["firstLogin"] ?? "operator";
_defaultLoginMode = ReadAppSetting("loginMode");
_defaultFirstLogin = ReadAppSetting("firstLogin");
_loginMode= _defaultLoginMode.Equals("2");
logger.Info("结束SettingWindowViewModel");
}
public DelegateCommand ResetConfigCommand
{
get => new DelegateCommand(() =>
{
var l = LoginMode ? "2" : "1";
if (!l.Equals(_defaultLoginMode))
{
Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
_configuration.AppSettings.Settings["loginMode"].Value = l;
_configuration.Save();
ConfigurationManager.RefreshSection("loginMode");
}
if (LoginMode && !FirstLogin.Equals(_defaultFirstLogin))
{
Configuration _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
_configuration.AppSettings.Settings["firstLogin"].Value = FirstLogin;
_configuration.Save();
ConfigurationManager.RefreshSection("firstLogin");
}
});
}
//手动实现调用配置的逻辑 规避修改配置文件后不起作用的问题
public string ReadAppSetting(string key)
{
string xPath = "/configuration/appSettings//add[@key='" + key + "']";
XmlDocument doc = new XmlDocument();
string exeFileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
doc.Load(exeFileName + ".dll.config");
XmlNode node = doc.SelectSingleNode(xPath);
return node.Attributes["value"].Value.ToString();
}
}
}