Created
January 27, 2012 14:24
-
-
Save biac/1689015 to your computer and use it in GitHub Desktop.
#win8 #metro でユーザーごとのデータを保存/復元する
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
// 抜き書きしたので、間違ってたらゴメン f(^^; | |
// try ~ catch も削ったし。 | |
private static async Task SaveImplAsync(UserDataRepository clonedData) | |
{ | |
//UserDataRepository clonedData; | |
//↑ここに、ユーザーごとのデータが入ってると思いねえ。 | |
// ※非同期でセーブするんだから、ナマ禁止! スナップショットをコピーしたのを使う。 | |
// Get the output stream for the SessionState file. | |
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); | |
IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite); | |
IOutputStream outStream = raStream.GetOutputStreamAt(0); | |
// Serialize the Session State. | |
DataContractSerializer serializer = new DataContractSerializer(typeof(UserDataRepository)); | |
serializer.WriteObject(outStream.AsStream(), clonedData); | |
await outStream.FlushAsync(); | |
} | |
private static async Task<UserDataRepository> RestoreImplAsync() | |
{ | |
// Get the input stream for the SessionState file. | |
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); | |
IInputStream inStream = await file.OpenForReadAsync(); | |
// Deserialize the Session State. | |
DataContractSerializer serializer = new DataContractSerializer(typeof(UserDataRepository)); | |
return serializer.ReadObject(inStream.AsStream()) as UserDataRepository; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あ、DataContractSerializer に扱わせるオブジェクトは、保存したいプロパティに [DataMember] 属性付けとけとか、イロイロ制約あるです。 f(^^;