/// <summary> /// Properties Dictionaryから復元する /// </summary> protected void LoadAppPreference() { if (App.Current.Properties.ContainsKey("AppPreference")) { var json = App.Current.Properties["AppPreference"] as string; Preference = AppPreference.FromJson(json); } else { Preference = new AppPreference(); } }
/// <summary> /// 永続性サポートユーティリティー インスタンス化 /// </summary> /// <param name="json"></param> /// <returns></returns> public static AppPreference FromJson(string json) { AppPreference ret = default(AppPreference); try { using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { var ser = new DataContractJsonSerializer(typeof(AppPreference)); ret = (AppPreference)ser.ReadObject(ms); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); ret = new AppPreference(); } return ret; }
/// <summary> /// 永続性サポートユーティリティー JSON化 /// </summary> /// <param name="instance"></param> /// <returns></returns> public static string ToJson(AppPreference instance) { string ret = null; try { using (var ms = new MemoryStream()) { var ser = new DataContractJsonSerializer(typeof(AppPreference)); ser.WriteObject(ms, instance); var bytes = ms.ToArray(); ret = Encoding.UTF8.GetString(bytes, 0, bytes.Length); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } return ret; }