个人资料

跳过导航链接首页 > 博客列表 > 博客正文

UWP使用和保存设置

分类:

帮助类代码:


    public partial class UserSetting 
    {
        public static string setting_filename = "user_setting.json";
        public static string music_volume = "background_music_volume";
        public static string sound_volume = "sound_effect_volume";
        public static string is_full_screen = "is_full_screen";
    }


    public partial class UserSetting
    {
        //
        public async static Task<UserSetting> ReadSetting()
        {
            StorageFolder folder = ApplicationData.Current.LocalFolder;
            var file = await folder.TryGetItemAsync(setting_filename);
            if (file != null)
            {
                try
                {

                    string settting_string = await FileIO.ReadTextAsync((StorageFile)file);
                    UserSetting setting = JsonConvert.DeserializeObject<UserSetting>(settting_string);
                    return setting;

                }
                catch (Exception)
                {
                    var default_setting = new UserSetting();
                    return default_setting;
                }



            }
            else
            {

                var default_setting = new UserSetting();
                return default_setting;
            }

        }

        public async static void SaveSetting(UserSetting setting)
        {
            string settting_string = JsonConvert.SerializeObject(setting);

            StorageFolder folder = ApplicationData.Current.LocalFolder;
            var file = await folder.CreateFileAsync(setting_filename, CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteTextAsync(file, settting_string);

        }





        public static void WriteSetting<T>(string key, T Tvalue)// where T : struct
        {
            ApplicationDataContainer root = ApplicationData.Current.LocalSettings;
            if (root.Values.TryGetValue(key, out object oldkey))
            {

                root.Values[key] = Tvalue;
            }
            else
            {
                root.Values.Add(key, Tvalue);
            }
        }





        public static T ReadSetting<T>(string key)
        {
            ApplicationDataContainer root = ApplicationData.Current.LocalSettings;

            if (root.Values.TryGetValue(key, out object value))
            {
                return (T)value;
            }
            else
            {
                return default(T);
            }
        }

        public static T ReadSetting<T>(string key,T default_value)
        {
            ApplicationDataContainer root = ApplicationData.Current.LocalSettings;

            if (root.Values.TryGetValue(key, out object value))
            {
                return (T)value;
            }
            else
            {
                return default_value;
            }
        }

    }
使用:



       public bool Is_full_screen
        {
            get {
                return UserSetting.ReadSetting<bool>(UserSetting.is_full_screen);
            }
            set {

                UserSetting.WriteSetting(UserSetting.is_full_screen, value);
            }
        }

        public double Music_volume
        {
            get
            {
                return UserSetting.ReadSetting<double>(UserSetting.music_volume,0.5);
            }
            set
            {
                UserSetting.WriteSetting(UserSetting.music_volume, value);
                OnPropertyChanged();
            }
        }

        public double Sound_volume
        {
            get
            {
                return UserSetting.ReadSetting<double>(UserSetting.sound_volume,0.5);
            }
            set
            {
                UserSetting.WriteSetting(UserSetting.sound_volume, value);
                OnPropertyChanged();
            }
        }


songshizhao
最初发表2022/3/20 0:46:32 最近更新2024/8/16 11:11:42 609
为此篇作品打分
10
   评论