将位图图像源绑定到我的本地文件夹中的图像

时间:2012-12-22 21:10:47

标签: windows-phone-8

我的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;
}

我的Xaml有以下代码:

<Image>
       <Image.Source>
           <BitmapImage UriSource="{Binding SelectedImage, Mode=OneWay}" CreateOptions="BackgroundCreation"/>
        </Image.Source>
</Image>

但我无法弄清楚要传递的正确字符串以显示图像 - 任何帮助都会受到赞赏!

TA

罗斯

1 个答案:

答案 0 :(得分:9)

使IsoStore数据绑定工作的最简单方法是将Image.Source数据绑定到Path属性,而不是Name属性。

private async void SetImage()
{
    var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
    this.DataContext = files.First();
}

XAML数据绑定:

 <Image x:Name="img" Source="{Binding Path}" Width="100" Height="100" />

这是Image.Source的打印屏幕,显示为StorageFile.Path:

Image.Source showing up to StorageFile.Path