Monotouch导入联系 - 退货类型问题

时间:2013-02-22 21:46:02

标签: ios xamarin.ios

我正在尝试让这个方法返回一个人,如果他们被选中。我的问题是我得到一个错误,说委托不能返回除void之外的类型,这是令人讨厌的,因为我不希望这个方法返回,除非这两个动作中的一个发生了。有什么建议吗?

public static ABPerson ImportContact ()
        {
            // Create placeholder for contact
            ABPeoplePickerNavigationController _contactController = new ABPeoplePickerNavigationController ();
            AppDelegate.navigation.PresentViewController (_contactController, true, null);

            _contactController.Cancelled += delegate {
                AppDelegate.navigation.DismissViewController (true, null);
                return;
            };

            _contactController.SelectPerson += delegate(object sender, ABPeoplePickerSelectPersonEventArgs e) {
                _importedContact = e.Person.GetEmails().FirstOrDefault;
                AppDelegate.navigation.DismissViewController (true, delegate {
                    return e.Person;
                });
            };
        }

2 个答案:

答案 0 :(得分:1)

您尝试在内部委托方法中返回函数结果。它无法实施。尝试使用公共事件。 I. e。:

    public event Action<ABPerson> OnPersonSelect;
    public void ImportContact ()
    {
        // Create placeholder for contact
        ABPeoplePickerNavigationController _contactController = new ABPeoplePickerNavigationController ();
        NavigationController.PresentViewController (_contactController, true, null);

        _contactController.Cancelled += delegate {
            NavigationController.DismissViewController (true, null);
            return;
        };

        _contactController.SelectPerson += delegate(object sender, ABPeoplePickerSelectPersonEventArgs e) {
            var _importedContact = e.Person;
            NavigationController.DismissViewController (true, delegate {
                if (OnPersonSelect != null)
                {
                    OnPersonSelect(_importedContact);
                }
            });
        };
    }

请注意,我将此方法标记为非静态,以便在我自己的测试样本中使用它。

答案 1 :(得分:1)

这就是我解决它的方法:

        private static void ImportContact ()
        {
            if (_contactController == null)
                _contactController = new ABPeoplePickerNavigationController ();
            AppDelegate.navigation.PresentViewController (_contactController, true, null);

            _contactController.Cancelled += delegate {
                if (!AppDelegate.navigation.PresentedViewController.IsBeingPresented)
                    AppDelegate.navigation.DismissViewController (true, null);
                return;
            };

            _contactController.SelectPerson += delegate(object sender, ABPeoplePickerSelectPersonEventArgs e) {
                if (!AppDelegate.navigation.PresentedViewController.IsBeingPresented) {
                    AppDelegate.navigation.DismissViewController (true, delegate { 
                        MapImportedContact (e.Person);
                    });
                }
                return;
            };
        }

在我从中映射字段后,我调用了一个方法

PopulateClientFields (Client mappedClient)

该方法将所有EntryElements设置为传入的值,然后使用以下命令重新加载视图:

clientsView.ReloadData ();

像魅力一样工作。对不起,我实际上并没有尽快发布。