title: saving state between pages when creating a windows 8 app
body: <p>I've been following the Create your first Windows Store app using C# or Visual Basic tutorials provided by Microsoft but am having some problems saving state when navigating between pages.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/apps/hh974581.aspx" rel="nofollow">Create your first Windows Store app using C# or Visual Basic</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/windows/apps/jj215600.aspx" rel="nofollow">Part 3: Navigation, layout, and views</a></p>
  
  <p>Basically I've noticed that if I navigate from the main page to the photo page select a photo, navigate back to the main page and then go to the photo page again it doesn't remember the photo that was selected.  I'm using the following code to navigate to the photo page from the main page. </p>
  
  <pre><code>private void photoPageButton_Click(object sender, RoutedEventArgs e)
  {
      this.Frame.Navigate(typeof(PhotoPage));
  }
  </code></pre>
  
  <p>In the photo page the loadstate method is</p>
  
  <pre><code>protected async override void LoadState(Object navigationParameter, Dictionary&lt;String, Object&gt; pageState)
  {
      if (pageState != null &amp;&amp; pageState.ContainsKey("mruToken"))
      {
          object value = null;
          if (pageState.TryGetValue("mruToken", out value))
          {
              if (value != null)
              {
                  mruToken = value.ToString();
  
                  // Open the file via the token that you stored when adding this file into the MRU list.
                  Windows.Storage.StorageFile file =
                      await Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruToken);
  
                  if (file != null)
                  {
                      // Open a stream for the selected file.
                      Windows.Storage.Streams.IRandomAccessStream fileStream =
                          await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
  
                      // Set the image source to a bitmap.
                      Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
                          new Windows.UI.Xaml.Media.Imaging.BitmapImage();
  
                      bitmapImage.SetSource(fileStream);
                      displayImage.Source = bitmapImage;
  
                      // Set the data context for the page.
                      this.DataContext = file;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The photo page save state is</p>
  
  <pre><code>protected override void SaveState(Dictionary&lt;String, Object&gt; pageState)
  {
      if (!String.IsNullOrEmpty(mruToken))
      {
          pageState["mruToken"] = mruToken; 
      }
  }
  </code></pre>
  
  <p>I've noticed that the pagestate is always null when navigated to.  Any ideas?</p>