2015-06-21 66 views
0

我有一個用於注入到我的服務的Windows Phone的IMvxFileStore實現的實例。在IsolatedStorageFileStream上不允許操作。當使用MvvmCross文件插件

讓我們說,我想存儲在位於AppSettings的文件設置\的UserProfiles \ profile1.txt

使用該文件的插件,我首先調用API EnsureFolder存在,傳遞的完整路徑我的個人資料設置文件appsettings \ userprofiles \ profile1.txt以確保此文件夾已創建並存在。

只是爲了理智,我檢查以確保該文件夾是使用FolderExist API創建的。至少現在至少返回true。

的代碼看起來是這樣的:

private string GetStorageItemFileName(string filename) 
     { 
      Guard.ThrowIfNull(string.IsNullOrWhiteSpace(BaseDirectory), Messages.RepositorBackingStoreIsNull); 
      filename = _fileStore.PathCombine(BaseDirectory, filename); 

      _fileStore.EnsureFolderExists(filename);  
      if (_fileStore.FolderExists(filename)) 
      { 
       // what do do???? 
      } 

      return filename; 
     } 

然而,當我試圖將內容寫入使用將writeToFile API文件,通過在文件名從上面的方法和一些返回的字符串,如下

try 
      { 
       _fileStore.WriteFile(filename, content); 
      } 
      catch (Exception ex) 
      { 
       Logger.Error(ex); 
      }  

,我得到以下異常:

System.IO.IsolatedStorage.IsolatedStorageException被捕獲
HResult = -2146233264 Message =不允許在 IsolatedStorageFileStream中執行操作。源= mscorlib程序堆棧跟蹤: 在System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(字符串路徑, 的FileMode模式,FileAccess的訪問,文件共享份額,緩衝區大小的Int32, IsolatedStorageFile ISF) 在System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor (字符串路徑, 的FileMode模式,IsolatedStorageFile ISF) 在Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore.WriteFileCommon(字符串 路徑,Action`1 streamAction) 在Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore。 WriteFile(字符串 路徑,字符串內容) at TrackuTransit.Core.Services.DataStore.StorageItemFileRepository.Put(String filena我,StorageItem數據)的InnerException:

我的開發環境的設置如下: - 表面臨3 - 的Visual Studio 2013社區 - 的Windows Phone 8.1 SDK和模擬器 - MvvmCross 3.0.0.4。 (是的,這是舊的,將在MVP後更新)。

在我深入研究MvvmCross代碼庫之前,任何有關我在這裏可能會做錯什麼的想法的人?

+0

在這一點上很難說,因爲你是在這樣一箇舊版本。我會盡力讓項目達到最新和最好的狀態,因爲它可能已經被照顧好了。這將刺激你,因爲你將需要改變你的PCL配置文件爲259或兼容。 – PkL728

+0

還要確保您沒有多個線程同時訪問隔離存儲。如果你確實需要將開放文件調用的最後一個參數設置爲System.IO.FileShare.ReadWrite或者你需要的任何權限。 – PkL728

+0

這是我希望不會聽到的答案:),因爲此時升級解決方案(包括28個項目)將讓我回到至少幾天。我懷疑這是一個跨線程問題。 –

回答

0

將項目升級到.NET 4.5並升級到MvvmCross 3.5.1,但仍遇到同樣的問題。雖然我因升級而失去了兩天,但我很高興它結束了。

我遇到了這裏的問題是與事實,我打電話

_fileStore.EnsureFolderExists(filename); 

和傳遞的完整路徑文件做。在幕後,MvvmCross正在呼叫

IsolatedStorageFile.CreateDirectory(filename); 

這似乎是創建一個具有相同文件名的目錄。因此,將「images \ image1.png」傳遞給上述API看起來會創建一個目錄或保存與「images \ images.png」相關的一些IO資源。

當您嘗試編寫使用

_fileStore.WriteFile(filename, content); 

MvvmCross試圖創建使用

var fileStream = new IsolatedStorageFileStream(path, FileMode.Create, isf)) 

文件流的異常被拋出在這裏提交。

修正是爲了確保您傳遞到IMvxFileStore.EnsureFolderExists API只是有問題的文件夾。在這種情況下,它是「圖像」。然後,您傳遞給IMvxFileStore.WriteFile文件的相對路徑,包括文件名,例如「images \ image1.png」,然後您就可以開始了。

固定的代碼如下所示:

private string GetStorageItemFileName(string filename) 
     { 
      Guard.ThrowIfNull(string.IsNullOrWhiteSpace(BaseDirectory), Messages.RepositorBackingStoreIsNull); 
      filename = _fileStore.PathCombine(BaseDirectory, filename); 

      // assumption is that filename does not contain directory information 
      _fileStore.EnsureFolderExists(BaseDirectory);  

      return filename; 
     }