Data binding 如何将图像绑定到刚从非应用文件夹中拾取的文件?

Data binding 如何将图像绑定到刚从非应用文件夹中拾取的文件?,data-binding,windows-runtime,microsoft-metro,winrt-xaml,Data Binding,Windows Runtime,Microsoft Metro,Winrt Xaml,用户从文件系统中选择图像。之后,我想在页面上显示此图像。我该怎么办?图像控件似乎无法访问拾取的文件。我是否应该将其复制到应用程序本地存储?正确吗?您不必复制文件存储文件对象引用了存储在设备中的文件。查看MSDN和《快速入门指南》了解更多信息 XAML <Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> <Image x:Name="img" Stretch="None" />` &

用户从文件系统中选择图像。之后,我想在页面上显示此图像。我该怎么办?图像控件似乎无法访问拾取的文件。我是否应该将其复制到应用程序本地存储?正确吗?

您不必复制文件<代码>存储文件对象引用了存储在设备中的文件。查看MSDN和《快速入门指南》了解更多信息

XAML

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
    <Image x:Name="img" Stretch="None" />`
</Grid>
private async Task SetImage()
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".gif");
    openPicker.FileTypeFilter.Add(".png");

    StorageFile file = await openPicker.PickSingleFileAsync();
    if(file != null)
    {
        BitmapImage bmp = new BitmapImage();

        using(var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            bmp.SetSource(stream);
        }
        img.Source = bmp;
    }
}