Created
January 21, 2017 03:09
-
-
Save 3t14/ac05beb4d700eaaa73b339e47d1226f6 to your computer and use it in GitHub Desktop.
JSON対応版ローカルストレージアクセス(PCLStorage)
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.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
using PCLStorage; | |
using Xamarin.Forms; | |
namespace HelloPCLStorage | |
{ | |
public class App : Application | |
{ | |
private Entry usernameEntry; | |
public App() | |
{ | |
var saveButton = new Button | |
{ | |
Text = "保存" | |
}; | |
usernameEntry = new Entry | |
{ | |
Placeholder = "Username" | |
}; | |
// The root page of your application | |
var content = new ContentPage | |
{ | |
Title = "HelloPCLStorage", | |
Content = new StackLayout | |
{ | |
VerticalOptions = LayoutOptions.Center, | |
Children = { | |
new Label { | |
HorizontalTextAlignment = TextAlignment.Center, | |
Text = "Welcome to Xamarin Forms!" | |
}, | |
usernameEntry, | |
saveButton | |
} | |
} | |
}; | |
saveButton.Clicked += async (sender, e) => | |
{ | |
await SaveSettings(); | |
}; | |
MainPage = new NavigationPage(content); | |
} | |
// | |
private string settingsFilename = "settings.dat"; | |
public async Task SaveSettings() | |
{ | |
// 保存先のフォルダインスタンスを生成 | |
IFolder rootFolder = FileSystem.Current.LocalStorage; | |
// 保存先のファイルを開く(存在いている場合は上書き | |
IFile file = | |
await rootFolder.CreateFileAsync( | |
settingsFilename, | |
CreationCollisionOption.ReplaceExisting); | |
// 単一項目のみなので、文字列をそのまま保存 | |
Dictionary<string, string> settings = new Dictionary<string, string>(); | |
// 辞書に追加 | |
settings.Add("username", usernameEntry.Text); | |
// JSONに変換 | |
string jsonString = JsonConvert.SerializeObject(settings); | |
Debug.WriteLine(jsonString); | |
await file.WriteAllTextAsync(jsonString); | |
} | |
public async Task LoadSettings() | |
{ | |
// 保存先のフォルダインスタンスを生成 | |
IFolder rootFolder = FileSystem.Current.LocalStorage; | |
ExistenceCheckResult result = await rootFolder.CheckExistsAsync(settingsFilename); | |
switch (result) | |
{ | |
case ExistenceCheckResult.FileExists: | |
// 存在する時だけ取得反映 | |
IFile file = await rootFolder.GetFileAsync(settingsFilename); | |
var jsonString = await file.ReadAllTextAsync(); | |
try | |
{ | |
var settings = | |
(Dictionary<string, string>)JsonConvert.DeserializeObject( | |
jsonString, | |
typeof (Dictionary<string, string>)); | |
usernameEntry.Text = settings["username"]; | |
} | |
catch (JsonException jex) | |
{ | |
// 失敗した場合 | |
Debug.WriteLine(jex.Message); | |
Debug.WriteLine(jsonString); | |
} | |
break; | |
case ExistenceCheckResult.FolderExists: | |
case ExistenceCheckResult.NotFound: | |
default: | |
break; | |
} | |
} | |
protected override async void OnStart() | |
{ | |
// Handle when your app starts | |
await LoadSettings(); | |
} | |
protected override void OnSleep() | |
{ | |
// Handle when your app sleeps | |
} | |
protected override void OnResume() | |
{ | |
// Handle when your app resumes | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment