我的ApplicationData.Current.LocalFolder文件夹中有一个图像列表.我想在Image控件中显示第一个图像.
在我的viewmodel类中,我有以下代码: -
StorageFolder folder = ApplicationData.Current.LocalFolder;
IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
if (files.Count > 0)
{
vm.SelectedImage = files[0].Name;
}
Run Code Online (Sandbox Code Playgroud)
我的Xaml有以下代码:
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding SelectedImage, Mode=OneWay}" CreateOptions="BackgroundCreation"/>
</Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚传递正确的字符串以显示图像 - 任何帮助将不胜感激!
钽
罗斯
使IsoStore数据绑定工作的最简单方法是将Image.Source数据绑定到Path属性,而不是Name属性.
private async void SetImage()
{
var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
this.DataContext = files.First();
}
Run Code Online (Sandbox Code Playgroud)
XAML数据绑定:
<Image x:Name="img" Source="{Binding Path}" Width="100" Height="100" />
Run Code Online (Sandbox Code Playgroud)
这是一个显示为StorageFile.Path的Image.Source的打印屏幕:

| 归档时间: |
|
| 查看次数: |
3926 次 |
| 最近记录: |