我正在使用 mvvmCross 在 Xamarin 中开发适用于 Windows phone 8.1 的应用程序。我需要从电话库中选择多个图像并显示它们。我正在使用 FileOpenPicker.SelectMultipleFilesAndContinue 这样做。现在我需要能够在视图中显示所有这些图像。一个问题是图像的最小数量必须是 20,并且图像可能非常大。首先,我尝试将它们制作成字节数组并使用转换器来显示它们。
public async void SelectFotosCallback(FileOpenPickerContinuationEventArgs args) {
if (args.Files.Count > 0) {
foreach (StorageFile file in args.Files) {
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
byte[] bytes = null;
using (var reader = new DataReader(fileStream.GetInputStreamAt(0))) {
bytes = new byte[fileStream.Size];
await reader.LoadAsync((uint)fileStream.Size);
reader.ReadBytes(bytes);
}
callback(bytes);
}
}
else {
}
}
这种方法在第一次尝试时似乎确实有效,但是当我尝试使用 5 张图像时,它就停止了工作。回调完成后,应用程序就退出了。没有错误信息或任何东西。(我的猜测是内存过载。)
在此之后,我找到了一个小解决方案,我将字节数组制作成 Xamarin.Form 图像。
public async void SelectFotosCallback(FileOpenPickerContinuationEventArgs args) {
if (args.Files.Count > 0) {
foreach (StorageFile file in args.Files) {
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
byte[] bytes = null;
using (var reader = new DataReader(fileStream.GetInputStreamAt(0))) {
bytes = new byte[fileStream.Size];
await reader.LoadAsync((uint)fileStream.Size);
reader.ReadBytes(bytes);
}
Image image = new Image();
image.Source = ImageSource.FromStream(() => new MemoryStream(bytes));
var iets = image.Source.BindingContext;
callback(image);
}
}
else {
}
这似乎解决了内存过载的问题。现在唯一的另一个问题是我似乎找不到任何显示这些图像的方式。
<GridView ItemsSource="{Binding SelectedImages}">
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<Image Style="{StaticResource imageListImage}" Source="{Binding Source}"/>
<Button Style="{StaticResource imageListXButton}">
<Button.Background>
<ImageBrush ImageSource="ms-appx:///Resources/XButton.png"/>
</Button.Background>
</Button>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
我尝试用简单的绑定来显示它们。我还没有找到任何可行的方法。有谁知道显示这些图像的方法,如果不知道,在不使用太多内存的情况下使用字节的最佳选择是什么。