Net 8.0 + MS Toolkit, MVVM, VS 2022, android emulator 11 API 30
I’ve a listview based on a ObservableCollection of class
public partial class ADVImage : BaseModel //<= extends ObservableObject
{
[ObservableProperty]
private int _ADVId;
[ObservableProperty]
private string _description;
[ObservableProperty]
private ImageSource _imgData;
}
here I’ve a command
public partial class ADVModel : BaseModel //<= extends ObservableObject
{
public ADVModel()
{
Images = new ObservableCollection<ADVImage>();
}
[ObservableProperty]
private string _titolo;
[ObservableProperty]
private string _testo;
public ObservableCollection<ADVImage> Images { get; set; }
[RelayCommand]
private async void AddImage() //Button in page "Add"
{
try
{
var result = await MediaPicker.Default.PickPhotoAsync();
if (result != null)
{
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
var newFile = Path.Combine(FileSystem.CacheDirectory, result.FileName);
using (var stream = await result.OpenReadAsync())
{
using (var newStream = File.OpenWrite(newFile))
{
await stream.CopyToAsync(newStream); // <=== HERE
}
ADVImage aDV = new ADVImage();
aDV.ImgData = ImageSource.FromFile(newFile);
aDV.Description = "New image";
Images.Add(aDV);
}
}
}
}
catch (Exception ex)
{
// The user canceled or something went wrong
}
}
}
When I reach the point where the stream has to be copied I receive this error:
“System.ObjectDisposedException: ‘Cannot access a closed Stream.'”
But why? I’ve never accessed
In android log I cannot find any error related…
Tryed to download MAUI official samples fron Github and copy-paste code from “PlatformIntegrationDemo”.. same issue/same error …
No solutions working.. chaged Mediapicker with FilePicker (same issue)