WP8.1 C#绑定接触式图像 [英] WP8.1 C# Binding Contact Image

查看:228
本文介绍了WP8.1 C#绑定接触式图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

资讯结果
很简单,我试图创建一个能够显示用户联系人的应用程序。

Information
Quite simply, I’m attempting to create an app that would display the contacts for a user.

我也是一个自学成才的程序员,所以我有在某些方面的编程经验,但我是比较新的一般的数据绑定。

I’m also a self-taught programmer, so I have experience with programming in some aspects, but I am relatively new to data-binding in general.

要开始,我有了一个图像中它绑定ListView控件。

To begin, I have a ListView control that has an image binding within it.

<ListView x:Name="ContactsView" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image x:Name="ContactImage" Source="{Binding Converter={StaticResource ContactPictureConverter}, Mode=OneWay}" Height="100" Width="100" Margin="0,0,0,0"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我也有一个转换器,获得接触式图像的IRandomAccessStream,并将其作为一个BitmapImage的。如果没有图像被找到(空的异常),则转换器将返回对触点的默认图像。

I also have a converter that gets an IRandomAccessStream of the Contact Image and returns it as a BitmapImage. If no image is found (null exception), then the converter will return back a default picture for contacts.

public class ContactPictureConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        Contact c = value as Contact;

        try
        {
            return GetImage(c).Result;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
        return @"Images/default.jpg";
    }

    async Task<BitmapImage> GetImage(Contact con)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.DecodePixelHeight = 100;
        bitmapImage.DecodePixelWidth = 100;
        using (IRandomAccessStream fileStream = await con.Thumbnail.OpenReadAsync())
        {
            await bitmapImage.SetSourceAsync(fileStream);
        }
        return bitmapImage;
    }

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

其他信息结果
该应用程序设置为Windows Phone的8.1 ​​WinRT的。结果
为了得到接触中,我用一个ContactStore的。

Additional Information
The app is set for Windows Phone 8.1 WinRT.
To get the contacts, I use a ContactStore.

IReadOnlyList<Contact> contacts; //Global Declaration

ContactStore store = await ContactManager.RequestStoreAsync();
contacts = await store.FindContactsAsync();
ContactsView.ItemsSource = contacts;

问题结果
(试图让接触式图像时,这意味着一些异常出现)每个触点返回默认联系人图像。因为大多数我的联系人都与它们相关联的图像这不应该发生。

Problem
Every contact is returning the default contact image (meaning some exception occurred when trying to get the contact image). This shouldn’t occur since majority of my contacts have images associated with them.

结果
我应该如何获得联系人的图像,并结合了对图像控制的Windows Phone 8.1?

Question
How should I get the image of a contact and bind that to an image control within a ListView for Windows Phone 8.1?

推荐答案

由于Romasz和Murven,我是能够得到解决我的问题。

Thanks to Romasz and Murven, I was able to get a solution for my problem.

XAML:

<ListView x:Name="ContactsView" Grid.Row="1" Background="White" ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Image x:Name="ContactImage" DataContext="{Binding Converter={StaticResource ContactPictureConverter}}" Source="{Binding Result}" Height="100" Width="100" Margin="0,0,0,0"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

转换器:

using Nito.AsyncEx;

public class ContactPictureConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        try
        {
            Contact c = value as Contact;
            return NotifyTaskCompletion.Create<BitmapImage>(GetImage(c));
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return null;
        }
    }

    private async Task<BitmapImage> GetImage(Contact con)
    {
        using (var stream = await con.Thumbnail.OpenReadAsync())
        {
            BitmapImage image = new BitmapImage();
            image.DecodePixelHeight = 100;
            image.DecodePixelWidth = 100;

            image.SetSource(stream);
            return image;
        }
    }

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

套餐:的结果
Nito.AsyncEx 从的NuGet

Packages:
Nito.AsyncEx from Nuget

这篇关于WP8.1 C#绑定接触式图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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