Building a Windows 8 Metro App, Part 3: Listing 2

The SaveAsync method in LocalStorage.

static async private Task SaveAsync()
{
    
    StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename,
    CreationCollisionOption.ReplaceExisting);
    
    IRandomAccessStream sessionRandomAccess = await
    sessionFile.OpenAsync(FileAccessMode.ReadWrite);
    
    IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
    
    DataContractSerializer sessionSerializer = new
    DataContractSerializer(typeof(Dictionary<string, object>));
    
    sessionSerializer.WriteObject(sessionOutputStream.AsStream(), _data);
    
    await sessionOutputStream.FlushAsync();
    
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.