1

是否可以在不使用类 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; }
    }
}

此 ObservableCollection 中的每个对象都表示每次用户从 EmailAddressChooserTask 中选择联系人时应用程序“缓存”的联系人图片。

调用此方法时需要

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 对象。

那可能吗?

谢谢!

4

2 回答 2

0

同步访问联系人是不可能的,因为它没有 API。

您可以在“加载”状态下创建视图模型,然后在异步方法完成时“填充”它。只要确保模型类实现INotifyPropertyChanged

于 2012-09-24T21:27:25.193 回答
0

感谢 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));
        }
    }

结果,显示的联系人没有图片,但几乎自动加载了图片。

我希望它有所帮助

于 2012-09-25T20:58:42.727 回答