■ ApplicationDataContainer 클래스의 Values 속성을 사용해 로컬 설정 값을 저장하거나 구하는 방법을 보여준다. (PACKAGED)
▶ 예제 코드 (C#)
1 2 3 4 5 |
LocalSettingHelper.SaveLocalSettingItem("exampleSetting", "Hello, World!"); string value = LocalSettingHelper.ReadLocalSettingItem("exampleSetting"); |
▶ LocalSettingHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
using Windows.Storage; /// <summary> /// 로컬 설정 헬퍼 /// </summary> public static class LocalSettingHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 애플리케이션 데이터 컨테이너 /// </summary> private static ApplicationDataContainer _applicationDataContainer; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Static #region 생성자 - LocalSettingHelper() /// <summary> /// 생성자 /// </summary> static LocalSettingHelper() { _applicationDataContainer = ApplicationData.Current.LocalSettings; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 로컬 설정 항목 저장하기 - SaveLocalSettingItem(key, value) /// <summary> /// 로컬 설정 항목 저장하기 /// </summary> /// <param name="key">키</param> /// <param name="value">값</param> public static void SaveLocalSettingItem(string key, string value) { _applicationDataContainer.Values[key] = value; } #endregion #region 로컬 설정 항목 읽기 - ReadLocalSettingItem(key) /// <summary> /// 로컬 설정 항목 읽기 /// </summary> /// <param name="key">키</param> /// <returns>설정 값</returns> public static string ReadLocalSettingItem(string key) { if(_applicationDataContainer.Values.TryGetValue(key, out object value)) { return value as string; } else { return null; } } #endregion } |