简体   繁体   English

如何使用Monotouch从IntPtr获取ABPerson

[英]How to get ABPerson from an IntPtr using Monotouch

Is there a way in Monotouch how to get ABPerson object from an IntPtr. Monotouch中有没有一种方法可以从IntPtr获取ABPerson对象。 I'm using ABPeoplePickerNavigationController and its ABPeoplePickerNavigationControllerDelegate. 我正在使用ABPeoplePickerNavigationController及其ABPeoplePickerNavigationControllerDelegate。 I have to read some properties of selected person in method ShouldContinue. 我必须在方法ShouldContinue中阅读选定人员的一些属性。 My code looks like this: 我的代码如下所示:

ABPeoplePickerNavigationController nc = new ABPeoplePickerNavigationController();
nc.Delegate = new CustomABPeoplePickerNavigationControllerDelegate();

And my custom delegate looks like this: 我的自定义委托看起来像这样:

public class CustomABPeoplePickerNavigationControllerDelegate : ABPeoplePickerNavigationControllerDelegate
{           
   public override bool ShouldContinue (ABPeoplePickerNavigationController peoplePicker, IntPtr selectedPerson)
   {
       // *** HERE I HAVE TO GET ABPerson FROM IntPtr ***
       peoplePicker.DismissModalViewControllerAnimated(false);
       return true;
   }
}   

Sadly the ABPerson constructor that accept an IntPtr is internal in MonoTouch. 可悲的是,接受IntPtr的ABPerson构造函数在MonoTouch中是内部的。 You can either: 您可以:

  • use reflection to call the .ctor 使用反射来调用.ctor
  • use another API to retrieve the ABPerson instance 使用另一个API检索ABPerson实例

but you cannot use inheritance to solve this since the base (ABRecord) .ctor is also internal. 但是您不能使用继承来解决此问题,因为基本(ABRecord).ctor也是内部的。

I'll look why this .ctor is internal (afaik many of such .ctor are public in MonoTouch) and, if possible (ie if there's no alternatives), fix this for upcoming releases. 我将研究为什么该.ctor是内部的(许多此类.ctor在MonoTouch中都是公开的),并且在可能的情况下(即,如果没有其他选择),请为以后的版本修复此问题。

EDIT: further reading suggest that you use the SelectPerson event on ABPeoplePickerNavigationController. 编辑:进一步阅读建议您在ABPeoplePickerNavigationController上使用SelectPerson事件。 This will use an internal delegate that will convert the IntPtr into a ABPerson instance, solving your issue :) 这将使用内部委托将IntPtr转换为ABPerson实例,从而解决您的问题:)

还没有尝试过,但是您可以:

ABPerson person = peoplePicker.AddressBook.SingleOrDefault(s => s.Handle == selectedPerson) as ABPerson;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM