问题
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...
回答1:
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
来源:https://stackoverflow.com/questions/39562861/uwp-storagefile-to-imagesource-obeying-mvvm