Skip to content

Instantly share code, notes, and snippets.

@biac
Created January 27, 2012 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biac/1689015 to your computer and use it in GitHub Desktop.
Save biac/1689015 to your computer and use it in GitHub Desktop.
#win8 #metro でユーザーごとのデータを保存/復元する
// 抜き書きしたので、間違ってたらゴメン 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;
}
@biac
Copy link
Author

biac commented Jan 27, 2012

あ、DataContractSerializer に扱わせるオブジェクトは、保存したいプロパティに [DataMember] 属性付けとけとか、イロイロ制約あるです。 f(^^;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment