信息
很简单,我试图创建一个应用程序,将显示用户的联系人。WP8.1 C#绑定联系人图片
我也是一个自学成才的程序员,所以我在某些方面有编程经验,但是我对数据绑定一般来说比较新。
首先,我有一个ListView控件,它有一个图像绑定。
<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返回。如果找不到图像(空例外),则转换器将返回联系人的默认图片。
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。
IReadOnlyList<Contact> contacts; //Global Declaration
ContactStore store = await ContactManager.RequestStoreAsync();
contacts = await store.FindContactsAsync();
ContactsView.ItemsSource = contacts;
问题
每个接触返回默认联系人图像(意味着一些异常时,试图获得接触式图像发生)。这不应该发生,因为我的大部分联系人都有与他们相关的图像。
问题
我应该如何得到联系人的图像,并结合,为Windows Phone的8.1一个ListView内的图像控制?
您能够以检索单个图像,只是把它作为一个图像的源?也不要让你的代码同步 - 更好地阅读[本文](http://msdn.microsoft.com/en-us/magazine/dn605875.aspx),也[此答案](http://stackoverflow.com/a/26150727/2681948)可能会有所帮助。 – Romasz 2014-12-07 10:26:37