ImageSource.FromStreamは常に作成されたストリームを解放するので、私は同じ問題を持っていた...ここで写真を撮ると、バインドさListViewコントロール内の画像を表示する私のコードです。要するに、私はプラグインから元のストリームを取得し、それをバイト[]に変換し、それを専用オブジェクトに格納します。ピクチャを画像ソースに表示する必要があるときは、byte []から新しいストリームを作成し、Xamarin.FormsのImageSource.FromStreamメソッドで使用します。
public class PictureModel
{
public byte[] File { get; set; }
public ImageSource ImageSource => ImageSource.FromStream(() => new MemoryStream(File));
}
public class PicturesViewModel : ViewModelBase
{
public ObservableCollection<PictureModel> Pictures { get; set; } = new ObservableCollection<PictureModel>();
public ICommand TakePictureCommand => new Command(async() =>
{
var stream = await TakePictureAsync();
var picture = new PictureModel { File = await ImageConverter.ReadFully(stream) }; // custom method to convert stream into byte[]
Pictures.Add(picture);
});
protected async Task<Stream> TakePictureAsync()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
return null;
}
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = false,
PhotoSize = PhotoSize.Medium
});
if (file == null)
return null;
var stream = file.GetStream();
file.Dispose();
return stream;
}
}
たぶん私は、私はまだテストしていない TakePictureCommandでローカルストリームを配置する必要がありますが。
とXAML:
<ListView x:Name="listView" ItemsSource="{Binding Pictures}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowHeight="200">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Image Source="{Binding ImageSource}" WidthRequest="400" HeightRequest="200" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
あなたがブレークポイントを入れましたか? 'image'がヌルか' file'かどうかを判断できますか? –
もっとコードを表示できますか?一行から何が起こっているのかを知るのは難しいです。 – Adam