我尝试使用插件Xamarin.Forms.Contacts
并遵循 github 链接,
https: //gist.github.com/enisn/25fd0a63a849854fb6103aa681be9963
但是,当我编译和调试时,屏幕上什么也没有显示。我也将插件添加到 Android 和 iOS 并设置了所需的权限。在GetContacts()
Debugger 的第一行死掉并且没有移动到另一行。
public ContactList()
{
InitializeComponent();
GetContacs();
}
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
async Task GetContacs()
{
var contacts = await Plugin.ContactService.CrossContactService.Current.GetContactListAsync();
lstContacts.BindingContext = contacts;
},
我还按照链接上的教程,https://www.xamboy.com/2019/10/10/getting-phone-contacts-in-xamarin-forms/ 在本教程中,我发现了权限功能的问题,调试器死了这里也是,在 if 条件下验证权限。
`public async Task<bool> RequestPermissionAsync()
{
contactPermissionTcs = new TaskCompletionSource<bool>();
// Verify that all required contact permissions have been granted.
if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(CrossCurrentActivity.Current.Activity, Manifest.Permission.ReadContacts) != (int)Permission.Granted
|| Android.Support.V4.Content.ContextCompat.CheckSelfPermission(CrossCurrentActivity.Current.Activity, Manifest.Permission.WriteContacts) != (int)Permission.Granted)
{
// Contacts permissions have not been granted.
RequestContactsPermissions();
}
else
{
// Contact permissions have been granted.
contactPermissionTcs.TrySetResult(true);
}
return await contactPermissionTcs.Task;
}`
这也没有奏效。
是否有任何建议或建议可以使我的任务更轻松?