几周前我刚开始学习如何开发一些非常简单的 Windows 8 应用程序,并想制作一个照片库应用程序,它可以从我的本地图片文件夹中选择多个图像,然后在 FlipView 中显示它们。
到目前为止,我发现的大多数教程都只是对下面的图像进行硬编码。
<FlipView x:Name="flipView1" SelectionChanged="FlipView_SelectionChanged">
<Image Source="Assets/Logo.png" />
<Image Source="Assets/SplashScreen.png" />
<Image Source="Assets/SmallLogo.png" />
</FlipView>
我从这个站点 ( http://msdn.microsoft.com/en-us/library/windows/apps/jj655411.aspx ) 开始了这个项目,并做了一些更改。目前这是我的 XAML 中的内容
<FlipView x:Name="flpView" Grid.Row="1" Margin="10, 10, 10, 10" SelectionChanged="FlipView_SelectionChanged">
<Image x:Name="image"/>
</FlipView>
这是我背后的代码。
private async void FlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Add code to perform some action here.
Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types.
openPicker.FileTypeFilter.Clear();
openPicker.FileTypeFilter.Add(".bmp");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".jpg");
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
foreach (StorageFile Images in files)
{
// file is null if user cancels the file picker.
if (Images != null)
{
// Open a stream for the selected file.
Windows.Storage.Streams.IRandomAccessStream fileStream =
await Images.OpenAsync(Windows.Storage.FileAccessMode.Read);
// Set the image source to the selected bitmap.
Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.SetSource(fileStream);
image.Source = bitmapImage;
this.DataContext = Images;
}
flpView.ItemsSource = Images; //This gave an excption
}
}
当我运行程序时会发生什么,我可以选择多个文件,但只能显示 1 张图片。我很确定会发生这种情况,因为我在 FlipView 中声明了一个图像,这会导致所选图像在该图像上重叠。如何让它们出现在 FlipView 中?