我正在使用mvvmCross在Xamarin中爲Windows Phone 8.1的應用程序工作。我需要從手機庫中選擇多個圖像並顯示它們。我正在使用FileOpenPicker.SelectMultipleFilesAndContinue來這樣做。現在我需要能夠在視圖中顯示所有這些圖像。一個問題是圖像的最小數量必須是20,並且圖像可能非常大。 首先,我試着把它們變成字節數組,並用轉換器來顯示它們。在windows phone中選擇並顯示xamarin.form.Image
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>
我嘗試用簡單的綁定顯示它們。我還沒有找到任何可行的方式。有誰知道一種方法來顯示這些圖像,如果不是什麼將是最好的選擇使用字節而不使用太多的內存。