The following is a quick tutorial on how to load a series of images from the machine and display them on screen. This is something that I’m working on for my next Store Application.
The XAML
Let’s start with the screen layout - the GridView control makes this quite an easy task in Windows 10:
<GridView ItemsSource="{Binding Images}">
<GridView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Stretch="Uniform"
Width="100" Height="100"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Binding
In this case, I have bound the view to a ViewModel; the relevant part of which looks like this:
private ObservableCollection<BitmapImage> \_images;
public ObservableCollection<BitmapImage> Images
{
get
{
if (\_images == null)
{
\_images = new ObservableCollection<BitmapImage>();
}
return \_images;
}
set
{
\_images = value;
NotifyPropertyChanged();
}
}
Basically, just an observable collection of BitmapImage.
Load the Image
Here’s where we populate the list:
private async void Load()
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".gif");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var fileStream = await file.OpenAsync(FileAccessMode.Read);
BitmapImage bi = new BitmapImage();
bi.SetSource(fileStream);
Images.Add(bi);
}
}
Conclusion
I spent a short period of time wondering what had happened to the UniformGrid for Windows 10, but as you can see, the GridView just makes this job much easier.