0

リンクImageCachingからクラスを見つけました が、分離ストレージからロードすると、「System.InvalidOperationException」という例外が発生しました。

ここに私のコードがあります

 public static object DownloadFromWeb(Uri imageFileUri)
    {
        WebClient m_webClient = new WebClient();                                //Load from internet
        BitmapImage bm = new BitmapImage();

        m_webClient.OpenReadCompleted += (o, e) =>
        {
            if (e.Error != null || e.Cancelled) return;
            WriteToIsolatedStorage(IsolatedStorageFile.GetUserStoreForApplication(), e.Result, GetFileNameInIsolatedStorage(imageFileUri));
            bm.SetSource(e.Result);
            e.Result.Close();
        };
        m_webClient.OpenReadAsync(imageFileUri);
        return bm;
    }

    public static object ExtractFromLocalStorage(Uri imageFileUri)
    {
        byte[] data;
        string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri);       //Load from local storage
        if (null == _storage)
        {
             _storage = IsolatedStorageFile.GetUserStoreForApplication();
        }
        using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
        {


            // Read the entire file and then close it
            sourceFile.Read(data, 0, data.Length);
            sourceFile.Close();
           BitmapImage bm = new BitmapImage();
            bm.SetSource(sourceFile);///here got the exeption 
            return bm;

        }

    }

そのため、画像を設定できません。

4

1 に答える 1

0

私はあなたが言及したコンバーターを使用しています、そしてそれは動作しますが、あなたはメソッドを変更しました。

ExtractFromLocalStorageメソッドは同じではありません。SetSourceメソッドで使用する前に、ストリームを閉じます。

元のメソッドコードは次のとおりです。

 private static object ExtractFromLocalStorage(Uri imageFileUri)
    {
        string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri);       //Load from local storage
        using (var sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
        {
            BitmapImage bm = new BitmapImage();
            bm.SetSource(sourceFile);
            return bm;
        }
    }
于 2013-01-20T13:46:45.720 に答える