DebugAppParam.cs 1.84 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using System.IO;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace Flyad7_WPF
{
    public class DebugAppParam : INotifyPropertyChanged
    {
        public bool HasTimeGrid { get; set; }
        public bool HasGrid { get; set; }
        public bool HasGridAdv { get; set; }

        public int FB_Pos1 { get; set; } = 1000;

        public int FB_Pos2 { get; set; } = 3000;

        public int PosLen { get; set; } = 8900;

        public event PropertyChangedEventHandler PropertyChanged;

        public DebugAppParam() 
        {
            Load();
            this.PropertyChanged += AutoSave;
        }

        private bool isIgnoreSave;
        private async void AutoSave(object sender, PropertyChangedEventArgs e)
        {
            if (isIgnoreSave)
                return;

            //if (this.GetType().GetProperty(e.PropertyName).GetCustomAttributes(typeof(JsonPropertyAttribute), false).Count() > 0)
            {
                isIgnoreSave = true;
                await Task.Delay(5000);//Task.Yield();
                Save();
                isIgnoreSave = false;
            }
        }
        public void Save()
        {
            try
            {
                string json = JsonConvert.SerializeObject(this, Formatting.Indented);
                File.WriteAllText("param.json", json);
            }
            catch {
            
            }
        }

        public void Load()
        {
            try
            {
                string json = File.ReadAllText("param.json");
                isIgnoreSave = true;
                JsonConvert.PopulateObject(json, this);
                isIgnoreSave = false;
            }
            catch { 
            
            }
        }


    }


}