2

我正在尝试将图像从网络保存到后台任务的隔离存储中,但它会抛出

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll

我正在使用这段代码

 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            StreamResourceInfo sri = null;
            Uri uri = new Uri(tempJPEG, UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.Result);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }

当它从应用程序运行时,它可以 100% 工作,但不能从后台任务运行。有关如何保存图像的任何提示?

4

1 回答 1

3

您需要调用此代码 Dispatcher.BeginInvoke() 因为 WriteableBitmap 需要在 UI 线程而不是后台线程上执行。见@http ://codeblog.vurdalakov.net/2012/02/solution-wp7-unauthorizedaccessexceptio.html

于 2013-01-02T21:23:14.470 回答