Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

My First Windows 8 Application – Metro Puzzle

Rate me:
Please Sign up or sign in to vote.
4.83/5 (52 votes)
7 Apr 2012Ms-PL4 min read 128.5K   6.4K   97  
My first Windows 8 application, Metro Puzzle.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Runtime.Serialization;
using System.IO;
using Windows.ApplicationModel;

class SuspensionManager
{
    static private Dictionary<string, object> sessionState_ = new Dictionary<string, object>();
    private const string filename = "_sessionState.xml";

    static public Dictionary<string, object> SessionState
    {
        get { return sessionState_; }
        
    }

    // Worker to workaround deadlocks.
    static async public Task SaveAsync()
    {
        
        await Windows.System.Threading.ThreadPool.RunAsync((wiSender) =>
        {
            SuspensionManager.SaveImplAsync().Wait();
        }, Windows.System.Threading.WorkItemPriority.Normal);
    }

    static async private Task SaveImplAsync()
    {
        // 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(Dictionary<string, object>));
        serializer.WriteObject(outStream.AsStream(), sessionState_);
        await outStream.FlushAsync();
    }

    // Worker to workaround deadlocks.
    static async public Task RestoreAsync()
    {
        await Windows.System.Threading.ThreadPool.RunAsync((wiSender) =>
        {
            SuspensionManager.RestoreImplAsync().Wait();
        }, Windows.System.Threading.WorkItemPriority.Normal);
    }

    static async private Task RestoreImplAsync()
    {
        // Get the input stream for the SessionState file.
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
        if (file == null) return;
        IInputStream inStream = await file.OpenForReadAsync();

        // Deserialize the Session State.
        DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>));
        sessionState_ = (Dictionary<string, object>)serializer.ReadObject(inStream.AsStream());
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions