0

私のアプリケーションには、画像への URL のリストがあります。そして、この画像をダウンロードして、Isolated Storage に保存する必要があります。

私がすでに持っているもの:

using (IsolatedStorageFile localFile = IsolatedStorageFile.GetUserStoreForApplication()) {
...
foreach (var item in MyList)
{
    Uri uri = new Uri(item.url, UriKind.Absolute);

    BitmapImage bitmap = new BitmapImage(uri);
    WriteableBitmap wb = new WriteableBitmap(bitmap);

    using (IsolatedStorageFileStream fs = localFile.CreateFile(GetFileName(item.url)))//escape file name
    {
        wb.SaveJpeg(fs, wb.PixelWidth, wb.PixelHeight, 0, 85);
    }
}
...
}

このコードは、ファイルの関数内に配置されていApp.xaml.csます。私は多くの解決策を試しましたが、この問題は「無効なクロススレッド アクセス」です。

どうすればそれを機能させることができますか?

4

3 に答える 3

0

@Mateusz Rogulski

WebClient を使用する必要があります。問題の解決策に従うことをお勧めします。ちょうど試して。

 public string YourMethod(string yoursUri)
 {
    BitmapImage image=new BitmapImage();
    WebClient client = new WebClient();
    client.OpenReadCompleted += async (o, args) =>
    {
        Stream stream = new MemoryStream();
        await args.Result.CopyToAsync(stream);
        image.SetSource(stream);
    };
    client.OpenReadAsync(new Uri(yoursUri));//if passing object than you can write myObj.yoursUri
    return image;
}

これでイメージが作成され、この関数を呼び出す場所で有効なチェックを使用して、isolatedStorage に保存できます。

于 2013-12-03T06:10:50.017 に答える