问题描述
我是Xamarin Platform的初学者,&试图读取xamarin形式的联系人.甚至尝试过使用Xamarin.Mobile,但是它给出了很多错误.找到另外1个插件,即Xamarin.contacts https://github.com/jamesmontemagno/Xamarin .插件/树/主/联系人但没有给出相关示例.请为所有3个平台使用Xamarin.contacts的相关示例提供帮助在此先感谢:)
I am a beginner in Xamarin Platform, & trying to read contacts in xamarin forms. Even tried using Xamarin.Mobile, But it gives lots of error. Found 1 more plugin i.e. Xamarin.contacts https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Contactsbut no relevant example is given. Please help with a relevant example using Xamarin.contacts for all 3 platformsThanks in advance :)
推荐答案
请记住Xam.Plugin.Contacts当前为Alpha版本.
Just keep in mind Xam.Plugin.Contacts is currently in alpha version.
这是我的实现.请注意,尽管有类名,但它不是真正的MVVM模式.我尽可能简化了代码,以提高可读性.
This is my implementation. Notice despite of class names, it is not a real MVVM pattern. I simplified the code as much as I could to help readability.
MainPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Contacts"
x:Class="Contacts.MainPage">
<ContentPage.BindingContext>
<local:MainViewModel x:Name="ViewModel" />
</ContentPage.BindingContext>
<ListView ItemsSource="{ Binding Contacts }">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{ Binding DisplayName }"
Detail="{ Binding Nickname }" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
MainViewModel.cs
public class MainViewModel
{
public ObservableCollection<Plugin.Contacts.Abstractions.Contact> Contacts { get; private set; }
public MainViewModel()
{
this.Contacts = new ObservableCollection<Plugin.Contacts.Abstractions.Contact>();
ReloadContacts();
}
public void ReloadContacts()
{
// Device may request user permission to get contacts access.
var hasPermission = CrossContacts.Current.RequestPermission()
.GetAwaiter()
.GetResult();
if (hasPermission)
{
this.Contacts.Clear();
List<Plugin.Contacts.Abstractions.Contact> contacts = null;
CrossContacts.Current.PreferContactAggregation = false;
if (CrossContacts.Current.Contacts == null)
{
return;
}
contacts = CrossContacts.Current.Contacts.ToList();
foreach (var contact in contacts)
{
this.Contacts.Add(contact);
}
}
}
}
希望这会有所帮助.
这篇关于如何阅读Xamarin形式的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!