Last active
August 29, 2015 14:03
-
-
Save tmyt/40a01ac5b0d773275a77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using Windows.ApplicationModel.Resources; | |
using Windows.Storage; | |
namespace App28 | |
{ | |
public class AppConfigBase<Impl> : INotifyPropertyChanged | |
where Impl : class, new() | |
{ | |
private T GetLocalValue<T>(string name, T defaultValue) | |
{ | |
try | |
{ | |
var values = ApplicationData.Current.LocalSettings.Values; | |
if (values.ContainsKey(name)) return (T)values[name]; | |
return defaultValue; | |
} | |
catch | |
{ | |
return defaultValue; | |
} | |
} | |
private void SetLocalValue<T>(string name, T value) | |
{ | |
try | |
{ | |
var settings = ApplicationData.Current.LocalSettings; | |
if (settings.Values.ContainsKey(name)) | |
{ | |
settings.Values[name] = value; | |
} | |
else | |
{ | |
settings.Values.Add(name, value); | |
} | |
} | |
catch | |
{ | |
} | |
} | |
private T GetRoamingValue<T>(string name, T defaultValue) | |
{ | |
try | |
{ | |
var values = ApplicationData.Current.RoamingSettings.Values; | |
if (values.ContainsKey(name)) return (T)values[name]; | |
return defaultValue; | |
} | |
catch | |
{ | |
return defaultValue; | |
} | |
} | |
private void SetRoamingValue<T>(string name, T value) | |
{ | |
try | |
{ | |
var settings = ApplicationData.Current.RoamingSettings; | |
if (settings.Values.ContainsKey(name)) | |
{ | |
settings.Values[name] = value; | |
} | |
else | |
{ | |
settings.Values.Add(name, value); | |
} | |
} | |
catch | |
{ | |
} | |
} | |
private T GetValueProxy<T>(string name, T defaultValue) | |
{ | |
var prop = GetType().GetRuntimeProperty(name); | |
var attr = prop.GetCustomAttribute(typeof(LocalValueAttribute)); | |
return attr != null ? GetLocalValue(name, defaultValue) : GetRoamingValue(name, defaultValue); | |
} | |
private void SetValueProxy<T>(string name, T value) | |
{ | |
var prop = GetType().GetRuntimeProperty(name); | |
var attr = prop.GetCustomAttribute(typeof(LocalValueAttribute)); | |
if (attr != null) SetLocalValue(name, value); | |
else SetRoamingValue(name, value); | |
} | |
protected T GetValue<T>([CallerMemberName] string name = null) | |
{ | |
return GetValueProxy(name, default(T)); | |
} | |
protected T GetValue<T>(T defaultValue, [CallerMemberName] string name = null) | |
{ | |
return GetValueProxy(name, defaultValue); | |
} | |
protected void SetValue<T>(T value, [CallerMemberName] string name = null) | |
{ | |
SetValueProxy(name, value); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void OnPropertyChanged([CallerMemberName] string name = null) | |
{ | |
var h = PropertyChanged; | |
if (h != null) h(this, new PropertyChangedEventArgs(name)); | |
} | |
private static Impl _instance; | |
public static Impl Instance { get { return _instance ?? (_instance = new Impl()); } } | |
private static ResourceLoader _loader; | |
public static ResourceLoader Loader { get { return _loader ?? (_loader = new ResourceLoader()); } } | |
public static string GetString(string key) | |
{ | |
return Loader.GetString(key); | |
} | |
protected AppConfigBase() | |
{ | |
} | |
protected virtual Impl CreateInstance() | |
{ | |
return null; | |
} | |
} | |
public class LocalValueAttribute : Attribute { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment