使用Xamarin.Mobile读取Xamarin Forms中的联系人时出错

时间:2017-03-06 17:41:26

标签: xamarin.android xamarin.forms xamarin.mobile

我在循环浏览Xamarin.Forms android项目中的联系人时遇到以下错误

“类型'的表达式System.Collections.Generic.IEnumerable 1[Xamarin.Contacts.Contact]' cannot be used for return type 'System.Linq.IQueryable 1 [Xamarin.Contacts.Contact]'”

有趣的是我在bugzilla中发现了同样的错误,但未提及解决方案。

有人可以帮我解决错误

https://bugzilla.xamarin.com/show_bug.cgi?id=35244

以下是方法:

    public async Task<IEnumerable<MobileUserContact>> All()
    {

        if (_contacts != null) return _contacts;

        var contacts = new List<MobileUserContact>();

        try
        {
            if (!await _book.RequestPermission())
            {
                Console.WriteLine("Permission denied");
                return;
            }

            foreach (Contact contact in _book.OrderBy(c => c.LastName))
            {
                Console.WriteLine("{0} {1}", contact.FirstName, contact.LastName);
                contacts.Add(new MobileUserContact(contact.FirstName, contact.LastName, ""));
            }

            return contacts;
        }
        catch (Exception ex)
        {

            throw;
        }
    }

它正在使用Xamarin.Mobile 0.7.1.0版本dll

我启用了Read_Contacts权限

1 个答案:

答案 0 :(得分:1)

如果这是Xamarin.Mobile包问题,也许您可​​以提交问题here。在这里,我无法解决此错误,但只能建议两个不使用此Xamarin.Mobile包来获取联系人的解决方法。

  1. 您可以尝试使用ContactsPlugin for Xamarin and Windows来获取联系人。
  2. 当您使用此MobileUserContact进行编码时,var contacts = new List<MobileUserContact>();应该是一个类,但您可以像使用此方法一样使用它:contacts.Add(new MobileUserContact(contact.FirstName, contact.LastName, ""));

    无论如何,我创建了一个试图重现您的问题的演示,以下代码在我身边正常工作:

    public List<MobileUserContact> contacts;
    public async Task<IEnumerable<MobileUserContact>> All()
    {
        contacts = new List<MobileUserContact>();
        try
        {
            if (!await CrossContacts.Current.RequestPermission())
            {
                return null;
            }
    
            foreach (var contact in CrossContacts.Current.Contacts.ToList())
            {
                contacts.Add(new MobileUserContact
                {
                    FirstName = contact.FirstName,
                    LastName = contact.LastName
                });
            }
            return contacts;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    

    MobileUserContact类很简单:

    public class MobileUserContact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    

    READ_CONTACTS权限外,我们还需要注意此插件仅针对API 23+并针对Android平台针对API 23+进行编译。

    使用此软件包的优点是所有代码都可以放在PCL中,但此软件包目前处于Alpha状态,目前尚不完全支持

    1. 另一种方法是使用DependencyService并使用Android平台的原生API来获取联系人。
    2. 原生Android项目中的代码可以是这样的:

      var cursor = Android.App.Application.Context.ContentResolver.Query(Phone.ContentUri, null, null, null, null);
      contacts = new List<MobileUserContact>();
      while (cursor.MoveToNext())
      {
          contacts.Add(new MobileUserContact
          {
              FirstName = cursor.GetString(cursor.GetColumnIndex(Phone.InterfaceConsts.DisplayName)),
              Num = cursor.GetString(cursor.GetColumnIndex(Phone.Number))
          });
      }