数週間前に非常に単純な Windows 8 アプリの開発方法を学び始めたばかりで、ローカルの Pictures フォルダーから複数の画像を選択して 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 で Image を宣言したため、選択したイメージがその 1 つの Image に重なってしまうため、これが発生することは間違いありません。それらを FlipView に表示するにはどうすればよいですか?