0
在我的應用程序中,我必須從遠程服務器下載所有圖像並將其顯示在列表視圖中。當我試圖從獨立存儲中提取圖像時已經有一個例外拋出異常「IsolatedStorageFileStream不允許操作」當從isloated存儲中讀取
「不允許操作上IsolatedStorageFileStream」
。這裏是我的代碼
public static object ExtractFromLocalStorage(Uri imageFileUri)
{
byte[] data;
string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri); //Load from local storage
if (null == _storage)
{
_storage = IsolatedStorageFile.GetUserStoreForApplication();
}
BitmapImage bi = new BitmapImage();
//HERE THE EXCEPTION OCCURS
using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
{
data = new byte[sourceFile.Length];
// Read the entire file and then close it
sourceFile.Read(data, 0, data.Length);
// Create memory stream and bitmap
MemoryStream ms = new MemoryStream(data);
// Set bitmap source to memory stream
bi.SetSource(ms);
sourceFile.Close();
}
return bi;
}
上述功能用於從獨立存儲得到位圖圖像,和我的模型類是
public class Details
{
public string id { get; set; }
public string name { get; set; }
public Uri imgurl { get; set; }
[IgnoreDataMember]
public BitmapImage ThumbImage{get;set;}
}
和我使用的是singlton類調用函數來獲取圖像。
public class SingleTon
{
static SingleTon instance = null;
private SingleTon()
{
}
public static SingleTon Instance()
{
if (instance == null)
{
instance = new SingleTon();
}
return instance;
}
public BitmapImage getImage(Uri uri)
{
lock (this)
{
BitmapImage image = new BitmapImage();
image = (BitmapImage)CacheImageFileConverter.ExtractFromLocalStorage(uri);
return image;
}
}
public void writeImageToIsolatedStorage(Uri imageUri)
{
lock (this)
{
CacheImageFileConverter.DownloadFromWeb(imageUri);
}
}
}
這是設置圖像爲對象的代碼
SingleTon singleton = SingleTon.Instance();
List<(Details > datalist = new Details()
foreach (Details items in DataList)
{
items.ThumbImage = singleton.getImage(items.imgurl);
datalist.add(items);
}
請任何一個幫助我一個解決方案。
似乎有太多的代碼,你正面臨的異常......至少添加評論,你會得到一個異常,並刪除瘋狂的空行數量... –
在哪一行你會得到錯誤? –
@MattLacey使用(IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath,FileMode.Open,FileAccess.Read))/ /這裏的豁免發生 – Sujiz