using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; namespace MultiLayout.UiModule { /// /// /// public class UIModuleParam : INotifyPropertyChanged { /// /// 控件唯一标示 /// public int ID { get; set; } /// /// /// /// protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// /// /// public event PropertyChangedEventHandler PropertyChanged; } /// /// /// /// public abstract class UIModuleParams : INotifyPropertyChanged where T : UIModuleParam { /// /// /// [Newtonsoft.Json.JsonIgnore] public string FilePath; /// /// /// public List Items { get; set; } = new List(); /// /// 只保存 IDs 的控件 /// /// public virtual void MatchParam(int[] IDs) { if (Items.RemoveAll(i => { return !IDs.Contains(i.ID); }) > 0) { Save(); } } /// /// 保存 /// /// public virtual bool Save() { try { string json = Newtonsoft.Json.JsonConvert.SerializeObject(this); File.WriteAllText(FilePath, json); } catch { return false; } return true; } /// /// 读取 /// /// public virtual bool Load() { try { string json = File.ReadAllText(FilePath); Newtonsoft.Json.JsonConvert.PopulateObject(json, this); return true; } catch { return false; } } /// /// /// /// protected void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// /// /// public event PropertyChangedEventHandler PropertyChanged; } }