2

コレクション内の各画像には、シリアル化されたファイル パスがあります。コレクションをロードするとき、ファイル パスからイメージをロードする必要があります。以下のコードは、IsolatedStorageFileStream が image.SetSource() に使用される IRandomAccessStream と互換性がないため、機能しません。

public BitmapImage Image
    {
        get
        {
            var image = new BitmapImage();
            if (FilePath == null) return null;

            IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FilePath, FileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication());

            image.SetSource(stream);

            return image;
        }
    }

これを達成するための代替コードはありますか?

4

1 に答える 1

1

WindowsRuntimeStreamExtensions.AsRandomAccessStream拡張メソッドを使用するだけです。

using System.IO;
...

using (var stream = new IsolatedStorageFileStream(
    FilePath, FileMode.Open, FileAccess.Read,
    IsolatedStorageFile.GetUserStoreForApplication()))
{
    await image.SetSourceAsync(stream.AsRandomAccessStream());
}

私がテストしたときSetSource、これはアプリケーションをブロックしていたので、SetSourceAsync.


次のように、Isolated Storage フォルダーに直接アクセスすることもできます。

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
    FilePath, CreationCollisionOption.OpenIfExists);

using (var stream = await file.OpenReadAsync())
{
    await image.SetSourceAsync(stream);
}
于 2015-12-28T21:33:18.900 に答える