0

更新

我添加了通常会引发错误的代码/函数。

有人可以帮忙吗?

在开始之前,我已经花了 20 多个小时研究这个问题。

我正在开发一个 Windows 手机应用程序,并且不断收到很多独立存储异常,尤其是“独立存储文件流不支持操作”。

情景

我有一个具有ProfilePictureUrl作为属性的对象,每次创建该对象的实例时,我都会从 Web 下载配置文件图像,然后将该图像存储到独立存储中。

示例代码

foreach(String url in urls)
{
    var profile = new MyClass()
    {
        ProfilePictureURL = url
    };

    profile.DownloadProfilePictureToLocalStorage(() =>
    {
        completed(profile);
    }, (ex) => { incomplete(ex); });
}

抛出异常的代码

if (isoFile.FileExists(saveAs))
    isoFile.DeleteFile(saveAs);

using (var isoFileStream = isoFile.CreateFile(saveAs))
{
    var width = wb.PixelWidth;
    var height = wb.PixelHeight;
    Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}

这是在LocalStorageManager类的DownloadImageToIsolatedStorage函数中。

这是管理我的 IsolatedStorage 存储的类

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Media.Imaging;

namespace Classes.Managers
{
    public delegate void GetImageCompletedDelegate(BitmapImage bmp);
    public delegate void GetImageNotCompletedDelegate(Exception ex);

    public delegate void SaveImageCompletedDelegate();
    public delegate void SaveImageNotCompletedDelegate(Exception ex);

    public delegate void DeleteImageCompletedDelegate();
    public delegate void DeleteImageNotCompletedDelegate(Exception ex);

    class LocalStorageManager
    {
        private static readonly LocalStorageManager _instance = new LocalStorageManager();

        public static LocalStorageManager Instance
        {
            get
            {
                return _instance;
            }
        }

        private bool m_IsBusy;
        public bool IsBusy
        {
            get { return m_IsBusy; }
            private set { m_IsBusy = value; }
        }

        private void GetImageFromIsolatedStorage(String name, GetImageCompletedDelegate completed, GetImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    BitmapImage img = new BitmapImage();
                    if (isoFile.FileExists(name))
                    {
                        using (IsolatedStorageFileStream fileStream = isoFile.OpenFile(name, FileMode.Open, FileAccess.Read))
                        {
                            img.SetSource(fileStream);
                        }
                    }
                    completed(img);
                }
            }
            catch (Exception ex) { notCompleted(ex); }
        }

        public void DownloadImageToIsolatedStorage(String url, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            try
            {
                this.IsBusy = true;
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
                httpRequest.BeginGetResponse((callback) =>
                {
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {

                        using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(callback);

                            var bi = new BitmapImage();
                            bi.SetSource(response.GetResponseStream());
                            var wb = new WriteableBitmap(bi);

                            if (isoFile.FileExists(saveAs))
                                isoFile.DeleteFile(saveAs);

                            using (var isoFileStream = isoFile.CreateFile(saveAs))
                            {
                                var width = wb.PixelWidth;
                                var height = wb.PixelHeight;
                                Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                            }
                            this.IsBusy = false;
                            completed();
                        }
                    });
                }, null);
            }
            catch (Exception e) { this.IsBusy = false; notCompleted(e); }
        }

        public void MoveFile(String source, String destination)
        {
            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoFile.FileExists(source))
                {
                    isoFile.MoveFile(source, destination);
                }
            }
        }

        private void WriteImageStreamToIsolatedStorage(Stream imageStream, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isolatedStorage.FileExists(saveAs))
                        isolatedStorage.DeleteFile(saveAs);

                    var fileStream = isolatedStorage.CreateFile(saveAs);
                    imageStream.CopyTo(fileStream);
                    fileStream.Close();
                    /*
                    BitmapImage bmp = null;
                    bmp.SetSource(imageStream);
                    var writeableBMP = new WriteableBitmap(bmp);
                    writeableBMP.SaveJpeg(fileStream, writeableBMP.PixelWidth, writeableBMP.PixelHeight, 0, 100);
                    fileStream.Close();*/
                }
                completed();
            }
            catch (Exception ex) { notCompleted(ex); }
        }

        private void DeleteImageFromIsolatedStorage(string imageName, DeleteImageCompletedDelegate completed, DeleteImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isolatedStorage.FileExists(imageName))
                        isolatedStorage.DeleteFile(imageName);
                    completed();
                }
            }
            catch (Exception e) { notCompleted(e); }
        }

    }
}

这是我的对象的类

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.Net;

namespace Classes.Model
{
    public class MyClass : INotifyPropertyChanged
    {
        private string m_ProfilePictureURL;
        public string ProfilePictureURL
        {
            get { return m_ProfilePictureURL; }
            set
            {
                if (m_ProfilePictureURL == value)
                    return;

                m_ProfilePictureURL = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ProfilePictureURL"));
            }
        }

        [IgnoreDataMember]
        public BitmapImage LocalProfilePicture
        {
            get
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    try
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.SetSource(isoFile.OpenFile("ProfilePic_" + this.UUID, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite));
                        return bitmapImage;
                    }
                    catch (Exception ex)
                    {
                        isoFile.Dispose();
                        return null;
                    }
                }
            }
        }

        public void DownloadProfilePictureToLocalStorage(SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            // Wait till its no longer busy
            while (LocalStorageManager.Instance.IsBusy) ;
            if (!String.IsNullOrWhiteSpace(this.ProfilePictureURL) && !String.IsNullOrWhiteSpace(this.UUID))
            {
                LocalStorageManager.Instance.DownloadImageToIsolatedStorage(this.ProfilePictureURL, "ProfilePic_" + this.UUID, () =>
                {
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("LocalProfilePicture"));
                    completed();
                }, (ex) => { notCompleted(ex); });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
4

0 回答 0