2013-05-09 49 views
0

你好,我想在isolatedStora从文件的路径的Windows Phone 7如何获得isolatedStorageFileStream.name(抛出异常......)

using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) 
    { 
     // Initialize the buffer for 4KB disk pages. 
     byte[] readBuffer = new byte[4096]; 
     int bytesRead = -1; 

     // Copy the thumbnail to isolated storage. 
     while ((bytesRead = e.Result.Read(readBuffer, 0, readBuffer.Length)) > 0) 
     { 
      targetStream.Write(readBuffer, 0, bytesRead); 
     } 
     targetStream.Close(); 

     _myObject.object_image = targetStream.Name ; 
    } 
} 

这与Windows Phone 8的,但不与工作完美寡妇电话7 在Windows Phone 7有一个异常被抛出

{System.MethodAccessException: Attempt to access the method failed: System.IO.IsolatedStorage.IsolatedStorageFileStream.get_Name() 
    at deepView.Model.HistoryHelper.<saveObjectToHistory>b__1(Object s, OpenReadCompletedEventArgs e) 
    at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e) 
    at System.Net.WebClient.OpenReadOperationCompleted(Object arg) 
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) 
    at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) 
    at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 
    at System.Delegate.DynamicInvokeOne(Object[] args) 
    at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) 
    at System.Delegate.DynamicInvoke(Object[] args) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) 
    at System.Windows.Threading.Dispatcher.OnInvoke(Object context) 
    at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args) 
    at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args) 
    at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult) 
} 

任何人可以帮助我吗?

编辑: 从哪里获得的图像,并将其保存到独立存储(这完全在WP8工程)和高光部分的完整代码是我试图让名并保存到数据库

WebClient client = new WebClient(); 
     client.OpenReadCompleted += (s, e) => 
     { 
      try 
      { 
       string fileName = "Shared/Media/object_image_6200.jpg"; 
       //Save thumbnail as JPEG to isolated storage. 
       using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (!isoStore.FileExists(fileName)) 
        { 
         using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) 
         {        
          // Initialize the buffer for 4KB disk pages. 
          byte[] readBuffer = new byte[4096]; 
          int bytesRead = -1; 

          // Copy the thumbnail to isolated storage. 
          while ((bytesRead = e.Result.Read(readBuffer, 0, readBuffer.Length)) > 0) 
          { 
           targetStream.Write(readBuffer, 0, bytesRead); 
          }        


          **_historyObject.object_image = targetStream.Name;** 
          targetStream.Close(); 
          HistoryClass existingHistoryObject = null; 
          existingHistoryObject = db.FindObjectByObjectId(GlobalVariables.responseObject.object_id); 
          if (existingHistoryObject == null) 
          { 
           db.HistoryObjects.InsertOnSubmit(_historyObject); 
           db.SubmitChanges(); 
          } 
         }        
        } 
       }      
      } 
      catch (Exception ex) 
      { 
       //EXCEPTION HANDLING 
      } 

     }; 
     //get object image for history view 
     client.OpenReadAsync(new Uri("http://example.de/object_6200.jpg", UriKind.Absolute)); 

......

但是,当执行到达target.Name

+0

不是targetStream.Name会不会与fileName一样?为什么不使用它呢? – 2013-05-09 09:11:30

+0

否...文件名为object_6200.jpg,targetStreamName为:\\ Applications \\ Data \\ 32C945F5-B5D7-4287-95CE-B814F446339F \\ Data \\ IsolatedStore \\ object_image_6200.jpg。 我需要完整的路径来显示图像的一个枢轴。 而在Windows Phone 8上,targetStream.Name是 C:\\ Data \\ Users \\ DefApps \\ AppData \\ {EF1C8978-ACB9-4231-97A7-6F2CFC10AC5C} \\ Local \\ object_image_6200.jpg – 2013-05-09 09:23:07

+0

Where你从文件中获取?我无法重现您的问题。尝试发布完整的方法代码。 – 2013-05-09 09:43:23

回答

2

无论问题的原因,我无法重现它抛出异常,您不需要完整的路径在UI中显示图像。您可以改用相对路径。

编辑:

好,使这项工作尝试以下操作:

添加一个新的属性,你的类型的BitmapImage的_historyObject

BitmapImage MyBitmap { get; set; } 

当你从数据库中加载数据在GetHistoryObjects(),从存储的文件路径加载位图字段:

MyBitmap = GetBitmap(object_image); 

    private BitmapImage GetBitmap(string path) { 
    var bi = new BitmapImage(); 
    using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { 
     using (var fileStream = myIsolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read)) { 
     bi.SetSource(fileStream); 
     } 
    } 
    return bi 
    } 

最后,将图像控件源属性绑定到MyBitmap而不是object_image。

+0

是的,但我如何使用它然后在数据透视中? 我从GetHistoryObjects()中的数据库中获取所有对象,并将其放在透视中: PivotContainer。ItemsSource = GetHistoryObjects(); – 2013-05-09 10:41:57

+0

和枢轴的定义如下: '<控件:枢轴 X:名称= “PivotContainer” ...> <控件:Pivot.ItemTemplate> <列表框VerticalContentAlignment =“Stretch”Horizo​​ntalContentAlignment =“Center”Horizo​​ntalAlignment =“Center”> ...' – 2013-05-09 10:44:55

+0

请参阅我的编辑 – 2013-05-09 11:12:23