using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BWP.WinFormControl.WeightControl_ { public class WeightContext { private static string loginConfigPath = Application.StartupPath + "\\WeightConfigs.xml"; private static WeightConfigs _configs; public static WeightConfig GetConfig(ApplyTo applyto) { var config = Configs.Configs.Find(x => x.ApplyTo == applyto); if (config == null) { config=new WeightConfig(); config.ApplyTo = applyto; Configs.Configs.Add(config); XmlUtil.SerializerObjToFile(Configs, loginConfigPath); } return config; } public static WeightConfigs Configs { get { if (_configs == null) _configs = CreateConfigs(); return _configs; } } static WeightConfigs CreateConfigs( ) { var configs = new WeightConfigs(); if (!File.Exists(loginConfigPath)) { XmlUtil.SerializerObjToFile(configs, loginConfigPath); } else { configs = XmlUtil.DeserializeFromFile(loginConfigPath); } return configs; } public static void Save(ApplyTo applyto) { var config = Configs.Configs.First(x=>x.ApplyTo== applyto); if (string.IsNullOrEmpty(config.WeightSet)) throw new Exception("请选择称型号"); if (string.IsNullOrEmpty(config.ComSet)) throw new Exception("请选择Com口"); if (config.RateSet == null) throw new Exception("请选择波特率"); if (config.BitSet == null) throw new Exception("请选择数据位"); if (string.IsNullOrEmpty(config.Format)) throw new Exception("请填写显示格式"); try { string.Format("{0:" + config.Format + "}", 1); } catch (Exception ex) { throw new Exception("显示格式填写错误\n" + ex.Message); } XmlUtil.SerializerObjToFile(Configs, loginConfigPath); } } public class WeightConfigs { public WeightConfigs() { Configs=new List(); } public List Configs { get; set; } } public class WeightConfig { public ApplyTo ApplyTo { get; set; } public string WeightSet { get; set; } public string ComSet { get; set; } public int? RateSet { get; set; } private int mBitSet = 8; public int BitSet { get { return mBitSet;} set { mBitSet = value; } } public string Format { get; set; } public decimal? Discont { get; set; } public int WeightType { get; set; } public decimal MinWeight { get; set; } public decimal MaxWeight { get; set; } } public enum ApplyTo { 胴体入库=1, 分割称重 = 2, 领料退料=3 } }