PrivacyInfoVM.cs 1.72 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Configuration;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel
{
    public partial class PrivacyInfoVM : ObservableObject
    {
        public Window Owner { get; set; }

        [ObservableProperty]
        private bool isEnable = false;

        [ObservableProperty]
        private string btnContent;

        /// <summary>
        /// 超时时间(秒)
        /// </summary>
        private readonly int Seconds = 30;

        [RelayCommand]
        public void OK()
        {
            try
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings["PrivacyInfoStatus"].Value = bool.TrueString;
                config.Save();
                ConfigurationManager.RefreshSection("appSettings"); //刷新
                Owner.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Error($"操作失败:{ex.Message}");
            }
        }

        [RelayCommand]
        public void Cancel()
        {
            Owner.Close();
        }

        public void Reload()
        {
            Application.Current.Dispatcher.InvokeAsync(async () =>
            {
                IsEnable = false;
                for (int i = 0; i < Seconds; i++)
                {
                    var index = Seconds - i;
                    BtnContent = $"同意({index}S)";
                    await Task.Delay(1000);
                }
                BtnContent = "同意";
                IsEnable = true;
            });
        }
    }
}