0

你好我想从isolatedStora中的文件中获取路径

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 但不适用于 Widows phone 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时抛出异常

4

1 回答 1

2

无论问题的原因是什么,我都无法重现它,您不需要完整路径即可在 UI 中显示图像。您可以改用相对路径。

编辑:

好的,要完成这项工作,请尝试以下操作:

_historyObject为您的 BitmapImage 类型添加一个新属性:

  BitmapImage MyBitmap { get; set; }

当您从数据库加载数据时,GetHistoryObjects(),从存储的文件路径加载 Bitmap 字段:

  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。

于 2013-05-09T10:41:13.273 回答