2014-01-17 114 views
2

在我的Windows Store應用程序中,我的存儲文件夾中有一些文件(圖像)。如何正確刪除存儲文件?

然後,當我想用​​我的圖像創建位圖是這樣的:

BitmapImage BitmapImage = new BitmapImage(new Uri("full_file_path")); 

而且我Image控制具有ImageSource = "{Binding Path=BitmapImage}"
所有的作品都很棒。但是,當我要刪除我的文件,我用:

BitmapImage = null; 

然後:

try 
    { 
     await storageFile.DeleteAsync(StorageDeleteOption.Default); 
    } 
    catch (UnauthorizedAccessException e) 
    { 
     // and I get exception here. 
    } 

問:我應該如何正確地刪除文件?

回答

0

這聽起來像你試圖刪除你加載的圖像文件。該文件保持鎖定狀態,直到圖像被丟棄。在我看來,我認爲你應該在加載後複製圖像,然後在圖像控制中使用該圖像副本。

您可以使用以下代碼來實現您的方案,甚至不需要在刪除前處理圖像,

BitmapImage bmpImage = new BitmapImage(); 
bmpImage.BeginInit(); 
Uri uri = new Uri(filePath,UriKind.RelativeOrAbsolute); 
bmpImage.UriSource = uri; 
bmpImage.CacheOption = BitmapCacheOption.OnLoad; 
bmpImage.EndInit(); 

看看是否有幫助。 :)

+0

謝謝,但我認爲Windows應用商店的應用程序不支持System.Windows.Media.Imaging(使用Windows.UI.Xaml.Media.Imaging;) – jimpanzer