我正在Xamarin.Forms项目中编写一个C#应用,该项目显示联系人姓名和街道地址。我无法从CNContact提取地址并将联系人地址分配给字符串。
这很明显,但是我被卡住了!
public List<Contact> GetContacts()
{
contactList = new List<Contact>();
var store = new Contacts.CNContactStore();
var ContainerId = new CNContactStore().DefaultContainerIdentifier;
var predicate = CNContact.GetPredicateForContactsInContainer(ContainerId);
var fetchKeys = new NSString[] { CNContactKey.Identifier, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.Birthday, CNContactKey.PostalAddresses, CNContactKey.ImageData };
NSError error;
var IPhoneContacts = store.GetUnifiedContacts(predicate, fetchKeys, out error);
foreach(var c in IPhoneContacts)
{
var contact = new Contact();
contact.FirstName = c.GivenName;
contact.FamilyName = c.FamilyName;
if(c.PostalAddresses.Length !=0)
{
contact.StreetAddress = CNPostalAddressFormatter.GetStringFrom(c.PostalAddresses, CNPostalAddressFormatterStyle.MailingAddress);
};
contactList.Add(contact);
}
return contactList;
}
[Fetching Existing Contacts在iOS中:
[[First,您需要在Info.plist
中添加关注权限:
<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>
[Second,您可以创建一个包含需求联系信息的模型,如下所示:
public class ContactModel
{
public IList PhoneNumbers { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
}
Third,创建一个函数以获取信息:
public List<ContactModel> ReadContacts()
{
var response = new List<ContactModel>();
try
{
//We can specify the properties that we need to fetch from contacts
var keysToFetch = new[] {
CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName
};
//Get the collections of containers
var containerId = new CNContactStore().DefaultContainerIdentifier;
//Fetch the contacts from containers
using (var predicate = CNContact.GetPredicateForContactsInContainer(containerId))
{
CNContact[] contactList;
using (var store = new CNContactStore())
{
contactList = store.GetUnifiedContacts(predicate, keysToFetch, out
var error);
}
//Assign the contact details to our view model objects
response.AddRange(from item in contactList
where item?.EmailAddresses != null
select new ContactModel
{
PhoneNumbers = item.PhoneNumbers,
GivenName = item.GivenName,
FamilyName = item.FamilyName
});
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return response;
}
Fourth,调用func:
List<ContactModel> contacts = ReadContacts();
ContactModel contactVm;
for (int i = 0; i < contacts.Count; i++)
{
contactVm = contacts[i];
Console.WriteLine("Contact is : " + contactVm.FamilyName);
Console.WriteLine("Contact is : " + contactVm.GivenName);
Console.WriteLine("Contact is : " + contactVm.PhoneNumbers);
}
第五,输出打印信息(来自iPhone模拟器),如下所示:
Contact is : Bell
Contact is : Kate
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
Contact is : Higgins
Contact is : Daniel
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
Contact is : Appleseed
Contact is : John
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
Contact is : Haro
Contact is : Anna
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
Contact is : Zakroff
Contact is : Hank
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
Contact is : Taylor
Contact is : David
Contact is : Contacts.CNLabeledValue`1[Contacts.CNPhoneNumber][]
您的问题是CNPostalAddressFormatter.GetStringFrom()
方法需要一个CNPostalAddress对象作为参数,但是由于PostalAddresses属性是CNLabeledValue<ValueType>
对象的数组,因此您正在传递单个联系人的所有地址。
您应该做的是遍历所有地址,或者默认情况下仅采用第一个地址。确实取决于您要实现的目标。
例如,这将获得第一个CNPostalAddress
:
contact.StreetAddress = CNPostalAddressFormatter.GetStringFrom(c.PostalAddresses[0].Value, CNPostalAddressFormatterStyle.MailingAddress);
此外,如果您想知道地址的标签(家庭,工作等),可以这样获得:
c.PostalAddresses[0].Label
然后,实际的CNPostalAddress
对象又是这个:
c.PostalAddresses[0].Value