我有一個表格來填寫資料,其中包括一個影像,然后進入一個串列視圖。當我單擊一個按鈕以獲取影像時,它可以作業并進入表單,但是當我單擊另一個按鈕將其添加到串列視圖時,錯誤 [System.ObjectDisposedException: '無法訪問已處理的物件。物件名稱:“流已關閉”] 出現。
謝謝你的幫助
當我按下添加影像按鈕時:
var ActionPhoto = await DisplayActionSheet("Ajouter une pièce-jointe depuis:", "Annuler", null, "Galerie", "Caméra");
switch (ActionPhoto)
{
case "Galerie":
var Galerie = await MediaPicker.PickPhotoAsync(new MediaPickerOptions { Title = "Choisir une image" });
if (Galerie != null)
{
var voirImageGalerie = await Galerie.OpenReadAsync();
Image_Photos.Source = ImageSource.FromStream(() => voirImageGalerie);
}
break;
case "Caméra":
var camera = await MediaPicker.CapturePhotoAsync();
if (camera != null)
{
var voirImageCamera = await camera.OpenReadAsync();
Image_Photos.Source = ImageSource.FromStream(() => voirImageCamera);
}
break;
}
當我按下 listView 的添加按鈕時:
App.listePosteNoteFrais.Add(new Data{PostePJ = Image_Photos.Source});
在我的資料類中:
public ImageSource PostePJ { get; set; }
我要添加到串列視圖中的內容:
<Image x:Name="Image_PostePJ" Source="{Binding PostePJ}" HeightRequest="150" WidthRequest="150" Grid.Row="0" Grid.Column="12"/>
uj5u.com熱心網友回復:
給定代碼:
ImageSource.FromStream(() => voirImageCamera)
引數為FromStream:
() => voirImageCamera
每次需要影像時執行。
例外訊息:
System.ObjectDisposedException: '無法訪問已處置的物件。物件名稱:'流已關閉。
告訴您流 ( voirImageCamera) 不再可用。
我不確定什么內部代碼正在處理該流。也許 MediaPicker 認為它不再需要了。或者可能是由于從一個影像源復制到另一個影像源。或有關 ListView 如何/何時訪問影像源的資訊。
如檔案 Xamarin.Essentials: Media Picker / General Usage中所見,使用 MediaPicker 的結果的一種安全方法OpenReadAsync是將流保存在本地檔案中,然后將該檔案用作影像源:
// save the file into local storage
var newFile = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
using (var stream = await photo.OpenReadAsync())
using (var newStream = File.OpenWrite(newFile))
await stream.CopyToAsync(newStream);
然后將影像源設定為該檔案:
Image_Photos.Source = ImageSource.FromFile(newFile);
, 的優點FromFile是它應該能夠在任何需要的時候打開檔案 - 沒有stream被保持打開狀態。
注意:檔案示例使用CacheDirectory. 根據情況,FileSystem.AppDataDirectory可能更合適(應無限期保留的檔案)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471351.html
標籤:图片 列表显示 xamarin.forms
