使用CrossMedia从设备照片库中选择每个设备后,在ListView Xamarin中显示绑定的图像
我使用CrossMedia插件成功地选择了PictureView模型中的图片,并将其作为字节[]存储在图片对象中。稍后,我想使用以下xaml代码将此图片显示在与该阵列不同的页面上:使用CrossMedia从设备照片库中选择每个设备后,在ListView Xamarin中显示绑定的图像,xamarin,Xamarin,我使用CrossMedia插件成功地选择了PictureView模型中的图片,并将其作为字节[]存储在图片对象中。稍后,我想使用以下xaml代码将此图片显示在与该阵列不同的页面上: <ListView.ItemTemplate > <DataTemplate> <ViewCell> <StackLayout Orientation="Vertical" Padding="20
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="20">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding MyImageSource}"></Image>
<StackLayout Orientation="Vertical">
<Label Text="{Binding PicTitle, StringFormat='Picture Title: {0}'}" FontAttributes="Bold"></Label>
<Label Text="{Binding PicComment, StringFormat='Comments:: {0}'}"></Label>
</StackLayout>
</StackLayout>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Clicked="MenuItem_OnClicked" Text="Remove" IsDestructive="True" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
将对象内的字节[]传输到绑定到XAML的ImageSource对象。第一次选择图片时,效果很好。但是,如果我继续并用另一张图片替换此选择,它将不起作用。即使我用try-and-catch包围它,并且不返回任何堆栈跟踪或崩溃位置的信息,它也会在执行结束时崩溃,并出现一个null异常
我认为这与这条河有关,但我不知道到底是什么。如果有人对它为什么会这样做有绝对的想法,那就太好了。我希望我能很好地解释这个错误。谢谢。您可以添加调用“ImageSourceFromByteArray”方法的函数吗?哦,我实际上通过显示文件中的图像来解决这个问题。所以,当我从服务器获取它们时,我首先将它们临时保存到文件中,然后从路径显示它们。这条流非常不稳定,而且总是无缘无故地出错。
private ImageSource ImageSourceFromByteArray(byte[] arr)
{
if (arr != null && arr.Length > 0)
{
var stream = new MemoryStream(arr);
return ImageSource.FromStream(() => stream);
}
return null;
}