![](/img/trans.png)
[英]How to get all contacts list after adding contact to my app contact store in Windows Phone 8.1 RT?
[英]Windows Phone: How to get a contact without using Contacts.SearchAsync task?
是否可以在不使用类Contacts及其SearchAsync方法的情况下获得联系? 我继续解释我的问题。
我有一个ObservableCollection
private ObservableCollection<ContactPictureItemModel> _ContactPictures;
像这样的ContactPictureItemModel
public class ContactPictureItemModel
{
private string _Email;
private byte[] _Picture;
private string _DisplayName;
public ContactPictureItemModel(string email, byte[] picture, string displayName)
{
this._Email = email;
this._Picture = picture;
this._DisplayName = displayName;
}
public string Email
{
get { return _Email; }
set { _Email = value; }
}
public byte[] Picture
{
get { return _Picture; }
set { _Picture = value; }
}
public string DisplayName
{
get { return _DisplayName; }
set { _DisplayName = value; }
}
}
每当用户从EmailAddressChooserTask中选择联系人时,此ObservableCollection中的每个对象都代表应用程序已“缓存”的联系人图片。
调用此方法时我需要
public ContactPictureItemModel GetContactPictureItem(string email, string displayName)
{
ContactPictureItemModel contactPictureResult;
foreach (ContactPictureItemModel contact in ContactPictures)
{
if (email.Equals(contact.Key))
{
contactPictureResult = contact;
break;
}
}
if (contactPictureResult == null)
{
//Retrieve contact using "email" parameter
}
return contactPictureResult;
}
并且在ObservableCollection中找不到联系人,以便能够使用参数“ email”和“ displayName”获取联系人,而无需使用任何异步任务。 我需要函数检索ContactPictureItemModel对象。
那可能吗?
谢谢!
由于没有API,因此无法同步访问联系人。
您可以在“加载”状态下创建视图模型,然后在异步方法完成时“填充”其中。 只要确保模型类实现了INotifyPropertyChanged
感谢Richard Szalay,我找到了解决方案。 我将解释它以帮助每个人像我一样陷入这些问题(对Java符号表示抱歉,这样代码会更短)
这些是具有代表联系人及其图片的对象的私有字段
public class ParticipantItemModel {
private string _Email;
private string _DisplayName;
private bool _Paid;
[XmlIgnore]
private BitmapImage _ContactPicture;
[...]
}
每个私人领域都有自己的财产来获取和设定自己的价值。 像这个
public string Email {
get { return _Email; }
set { _Email = value; }
}
处理_ContactPicture时,其属性有些特殊。 为了获得其值,如果_ContactPicture为null且图片未缓存到ObservableCollection中,则使用Contacts.SearchAsync任务搜索联系人,然后返回“空图像”。 找到联系人后(尽管视图已加载),我将属性ContactPicture设置为正确的图像,并引发PropertyChangedEventArgs事件(如其setter属性所示)
[XmlIgnore]
public BitmapImage ContactPicture
{
get {
if (_ContactPicture != null) {
return _ContactPicture;
} else {
BitmapImage contactPictureSource = App.ContactPictures.GetContactPicture(Email, DisplayName);
if (contactPictureSource != null) {
return contactPictureSource;
} else {
Contacts contacts = new Contacts();
contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contacts_SearchCompleted);
contacts.SearchAsync(DisplayName, FilterKind.DisplayName, Email);
return new BitmapImage();
}
}
}
set {
_ContactPicture = value;
//When _ContactPicture is setted, an event is raised by calling to NotifyPropertyChanged()
NotifyPropertyChanged("ContactPicture");
}
}
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) {
Contact contact = null;
foreach (var result in e.Results) {
foreach (ContactEmailAddress contactEmail in result.EmailAddresses) {
if (Email.Equals(contactEmail.EmailAddress)) {
contact = result;
this.ContactPicture = GetSourceImageFromContactPicture(contact.GetPicture());
break;
}
}
}
}
}
另外,必须实现INotifyPropertyChanged接口以引发事件PropertyChangedEventArgs。 此事件将使应用程序知道元素(联系人的图片)已更改,并将使用绑定的数据刷新
public class ParticipantItemModel : INotifyPropertyChanged {
[...]
// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;
// NotifyPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
结果,显示的联系人没有图片,但几乎自动加载了图片。
希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.