ManIkWeet
ManIkWeet

Reputation: 1338

UWP - StorageFile to ImageSource obeying MVVM

I have multiple StorageFiles available as Task<StorageFile>s in my ViewModel, I want to bind an Image in my View to it.

A BitmapImage shouldn't be made in the ViewModel, because it's in the XAML namespace and requires the UI thread (which I fail to do).

How should I tackle this problem? Using a ValueConverter can't be done, as opening the StorageFile is async...

PS: I can't use an URI, the StorageFile is located in the LocalCache folder...

Upvotes: 1

Views: 819

Answers (1)

Andrii Krupka
Andrii Krupka

Reputation: 4306

Try just use Path property from StorageFile class:

<ListView ItemsSource="{Binding ImageItems}"
            Grid.Row="1">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Path}"/>
                <Image Grid.Row="1">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding Path}"/>
                    </Image.Source>
                </Image>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>

Where ImageItems it's public List<StorageFile> ImageItems property

Upvotes: 4

Related Questions