绑定设备图像路径 windows phone 8.1 [英] binding device image path windows phone 8.1

查看:21
本文介绍了绑定设备图像路径 windows phone 8.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绑定设备图片路径windows phone.

How to bind device image path windows phone.

下面是图片路径

"C://Data//Users//Public//Pictures//Camera Roll//WP_20141001_002.jpg"

谢谢

推荐答案

我不确定在您的情况下使用 string 是否是一个不错的选择 - 也许可以使用 BitmapImage - 从路径获取一个 StorageFile,打开 Stream 然后设置 BitmapImage - 在这种情况下你执行 async 转换器外的操作.

I'm not sure if in your case using string is a good choice - maybe it will be possible to use BitmapImage - obtain a StorageFile from path, open Stream and then set BitmapImage - in this case you perform async operations outside converter.

如果您仍然想使用 string,这是可能的,但需要一些特殊的方法 - 使用 async 方法和绑定.有一篇关于异步 MVVM 的非常好的文章,由 Stephen Cleary 撰写.根据文章和其他斯蒂芬的回答,我编写了这样的代码:

In case you still want to use string it's possible, but will need some special approach - using async methods along with binding. There is a very good article about aynchronous MVVM, written by Stephen Cleary. Basing on the article and other Stephen's answer I've made such a code:

首先,我们必须定义一个转换器 - 它并不复杂,因为获取文件和流是异步的:

First of all, we will have to define a Converter - it's little complicated as getting file and stream is asynchronous:

/// <summary>
/// Converter getting an image basing upon delivered path
/// </summary>
public class PathToImage : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var task = GetImage((String)value);
        // the below class you will find in Stephen's answer mentioned above
        return new TaskCompletionNotifier<BitmapImage>(task);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    { throw new NotImplementedException(); }

    private async Task<BitmapImage> GetImage(string path)
    {
        StorageFile file = await StorageFile.GetFileFromPathAsync(path);
        using (var stream = await file.OpenAsync(FileAccessMode.Read))
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(stream);
            return image;
        }
    }
}

在我们的页面中,我们需要一个属性,我们将在绑定和设置 DataContext 中使用它:

In our page we will need a property, which we will use in binding and set the DataContext:

public sealed partial class MainPage : Page, INotifyPropertyChanged
{
    private string imagePath;
    public string ImagePath
    {
        get { return imagePath; }
        set { imagePath = value; RaiseProperty("ImagePath"); }
    }

    public MainPage()
    {
        this.InitializeComponent();
        DataContext = this;
    }
    // rest of the code

当然,我们必须定义我们的绑定 - 例如在 XAML 中,这有点棘手,因为首先我们必须将 DataContext 绑定到我们的任务,然后将 Source 绑定到结果,结果将在加载图像时引发:

Of course we have to define our binding - for example in XAML, it's little tricky as first we have to bind the DataContext to our Task then bind Source to the Result, which will be raised as the image is loaded:

<Image DataContext="{Binding ImagePath, Converter={StaticResource PathToImage}}" Stretch="Uniform"
       Source="{Binding Result} HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

一旦我们有了这一切,我们可以像这样设置属性:

Once we have this all, we can set the property like this:

ImagePath = @"C:\Data\Users\Public\Pictures\Camera Roll\WP_20141001_002.jpg";

我们应该会在屏幕上看到结果.

and we should see the result on the screen.

这篇关于绑定设备图像路径 windows phone 8.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆