﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>WinStoreSaveLoadAppData</Title>
      <Author>matthias.shapiro@outlook.com</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>WinStore_SaveLoad_AppData</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>fileVar</ID>
          <ToolTip>File variable for the path to the saved item</ToolTip>
          <Default>_myFileLocation</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>filePath</ID>
          <ToolTip>File name for data we want to save</ToolTip>
          <Default>MyFileLocation.txt</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>objectName</ID>
          <ToolTip>Name the data you're about to save so you can recognize it when you call these methods</ToolTip>
          <Default>MyData</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>objectType</ID>
          <ToolTip>The object type that you are trying to save (CAUTION: Make sure this object is serializable)</ToolTip>
          <Default>List&lt;string&gt;</Default>
          <Function>
          </Function>
        </Literal>        
        <Literal Editable="true">
          <ID>objectInitial</ID>
          <ToolTip>If there is no existing file, return a new instance of that object</ToolTip>
          <Default>new List&lt;string&gt;();</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>saveObjectVar</ID>
          <ToolTip>A variable for the data you're about to save</ToolTip>
          <Default>saveData</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Delimiter="$">
        <![CDATA[#region Saving / Loading $objectName$ 
private const string $fileVar$ = "$filePath$";

public static async Task<$objectType$> Get$objectName$()
{ 
    // If you're saving your stuff just on this device
    var readStream = 
        await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync($fileVar$);

    // If there is no $objectName$, then we haven't created our $objectName$ file yet
    if (readStream == null)
        return $objectInitial$;

    DataContractSerializer stuffSerializer = 
        new DataContractSerializer(typeof($objectType$));

    var setResult = ($objectType$)stuffSerializer.ReadObject(readStream);

    return setResult;
}

public static async Task<bool> Save$objectName$($objectType$ $saveObjectVar$)
{
    try {

        StorageFile savedStuffFile = 
            await ApplicationData.Current.LocalFolder.CreateFileAsync($fileVar$, 
            CreationCollisionOption.ReplaceExisting);

        using (Stream writeStream = 
            await savedStuffFile.OpenStreamForWriteAsync())
        {
            DataContractSerializer stuffSerializer = 
                new DataContractSerializer(typeof($objectType$));

            stuffSerializer.WriteObject(writeStream, $saveObjectVar$);
            await writeStream.FlushAsync();
            writeStream.Dispose();
        }
    return true;
    }
    catch (Exception e)
    {
        throw new Exception("ERROR: unable to save $objectName$", e);
        //return false;
    }
}
#endregion]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>