UWP - StorageFile to ImageSource obeying MVVM

流过昼夜 提交于 2019-12-12 02:46:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!