Microsoft が提供する「C# または Visual Basic を使用して初めての Windows ストア アプリを作成する」チュートリアルに従っていますが、ページ間を移動するときに状態を保存するときに問題が発生します。
C# または Visual Basic を使用して最初の Windows ストア アプリを作成する
基本的に、メイン ページから写真ページに移動して写真を選択し、メイン ページに戻ってからもう一度写真ページに移動すると、選択した写真が記憶されていないことに気付きました。次のコードを使用して、メイン ページから写真ページに移動しています。
private void photoPageButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PhotoPage));
}
写真ページでは、loadstate メソッドは
protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
if (pageState != null && 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;
}
}
}
}
}
写真ページの保存状態は
protected override void SaveState(Dictionary<String, Object> pageState)
{
if (!String.IsNullOrEmpty(mruToken))
{
pageState["mruToken"] = mruToken;
}
}
ナビゲートすると、pagestate が常に null になることに気付きました。何か案は?