如何在Xamarin.ios中以字符串形式获取CNContact电话号码?

问题描述 投票:0回答:1

我正在尝试检索所有联系人的姓名和电话号码,并将其显示在Xamarin.iOS的表格视图中。我已经做到了:

 var response = new List<ContactVm>();
            try
            {
                //We can specify the properties that we need to fetch from contacts  
                var keysToFetch = new[] {
            CNContactKey.PhoneNumbers, CNContactKey.GivenName, CNContactKey.FamilyName, CNContactKey.EmailAddresses
            };
                //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 ContactVm
                                      {
                                          PhoneNumbers =item.PhoneNumbers,
                                          GivenName = item.GivenName,
                                          FamilyName = item.FamilyName,
                                          EmailId = item.EmailAddresses.Select(m => m.Value.ToString()).ToList()
                                      });



                }


                BeginInvokeOnMainThread(() =>
                {
                    tblContact.Source = new CustomContactViewController(response);
                    tblContact.ReloadData();

                });
            }

            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

这是我的更新单元格方法

 internal void updateCell(ContactVm contact)
        {
            try
            {

                lblName.Text = contact.GivenName;
                lblContact.Text = ((CNPhoneNumber)contact.PhoneNumbers[0]).StringValue;

                //var no = ((CNPhoneNumber)contact.PhoneNumbers[0]).StringValue;
                //NSString a = new NSString("");
                // var MobNumVar = ((CNPhoneNumber)contact.PhoneNumbers[0]).ValueForKey(new NSString("digits")).ToString();

                var c = (contact.PhoneNumbers[0] as CNPhoneNumber).StringValue;
            }
            catch(Exception ex)
            {
                throw ex;
            }


        }

我想知道如何仅将电话号码作为字符串值(即“ XXXXXXXXXX”)进行检索。基本上,如何调用数字值。这行代码

lblContact.Text = ((CNPhoneNumber)contact.PhoneNumbers[0]).StringValue;

抛出运行时异常,因为指定的转换无效

xamarin xamarin.ios
1个回答
0
投票

是的,例外是正确的。首先,您根本不需要任何类型转换,contact.PhoneNumbers [0]将返回您CNLabeledValue,因此您只需要用下面的方式编写]

lblContact.Text = contact.PhoneNumbers[0].GetLabeledValue(CNLabelKey.Home).StringValue
//or
lblContact.Text = contact.PhoneNumbers[0].GetLabeledValue(CNLabelPhoneNumberKey.Mobile).StringValue
© www.soinside.com 2019 - 2024. All rights reserved.