I have an UWP application that is supposed to display images from a disk in a grid. The image paths are being fetched from a database at runtime. It's not guaranteed that the user knows where the images to be displayed are, so using a folder picker won't work.
I already gave the app the broadFileSystemAccess capability. Binding with the absolute path still doesn't seem to work. The only kind of working solution I have for now is to create BitmapImages with a stream as the Source right after I'm getting the list of paths. However this comes with a serious memory problem. Does anyone has a solution for me?
relevant part of the ImageGalleryPage.xaml:
<GridView
Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="5"
Padding="{StaticResource MediumLeftRightMargin}"
animations:Connected.ListItemElementName="thumbnailImage"
animations:Connected.ListItemKey="galleryAnimationKey"
IsItemClickEnabled="True"
ItemsSource="{x:Bind ViewModel.Source, Mode=OneWay}"
SelectionMode="None">
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="ItemClick">
<ic:InvokeCommandAction Command="{x:Bind ViewModel.ItemSelectedCommand}" />
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:GalleryImage">
<Image
x:Name="thumbnailImage"
AutomationProperties.Name="{Binding Name}"
ToolTipService.ToolTip="{Binding Name}"
Source="{x:Bind Mode=OneWay, Path=ImgBitmapImage}"
Style="{StaticResource ThumbnailImageStyle}" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
relevant part of the ImageGalleryPage.xaml.cs:
public ImageGalleryPage()
{
InitializeComponent();
Loaded += ImageGalleryPage_Loaded;
}
private async void ImageGalleryPage_Loaded(object sender, RoutedEventArgs e)
{
Combo_Labels.ItemsSource = await SqlServerClassnamesService.FetchClassnames();
await ViewModel.LoadDataAsync();
}
ImageGalleryViewModel.cs:
public class ImageGalleryViewModel : ObservableObject
{
public ObservableCollection<GalleryImage> Source { get; } = new ObservableCollection<GalleryImage>();
public async Task LoadDataAsync()
{
Source.Clear();
var data = await SqlServerDataService.GetCollection();
foreach (var item in data)
{
await HandleImages(item);
}
}
private async Task HandleImages(GalleryImage img)
{
StorageFile file = await StorageFile.GetFileFromPathAsync(img.Path);
var stream = await file.OpenAsync(FileAccessMode.Read);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(stream);
img.Image = bitmapImage;
Source.Add(img);
}
}
GalleryImage.cs:
public class GalleryImage
{
public string ID { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public BitmapImage Image { get; set; }
public BitmapImage ImgBitmapImage
{
get => Image != null ? Image : new BitmapImage(new Uri(Path));
}
}