个人资料

主页 博客 留言板 搜索
跳过导航链接首页 > 博客列表 > 博客正文

UWP读写设置

分类


        /// <summary>
        /// 写入存储信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="Tvalue"></param>
        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);
            }
        }
        /// <summary>
        /// 有条件的写入存储信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="Tvalue"></param>
        public static void WriteSettingIfBigger<T>(string key, T Tvalue)
        {
            ApplicationDataContainer root = ApplicationData.Current.LocalSettings;
            if (root.Values.TryGetValue(key, out object oldvalue))
            {
                if (Convert.ToDouble(oldvalue) < Convert.ToDouble(Tvalue))
                {
                    root.Values[key] = Tvalue;
                }
            }
            else
            {
                root.Values.Add(key, Tvalue);
            }
        }

        /// <summary>
        /// 读取存储信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        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);
            }
        }


songshizhao
最初发表2018/9/8 17:56:52 最近更新2018/9/8 17:56:52 248
为此篇作品打分
10