1

如果他们被选中,我试图让这个方法返回一个人。我的问题是我收到一条错误消息,指出委托不能返回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;
                });
            };
        }
4

2 回答 2

1

您正在尝试在内部委托方法中返回函数结果。不可能实现。尝试改用公共事件。IE。:

    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);
                }
            });
        };
    }

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

于 2013-02-23T13:26:07.357 回答
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 ();

像魅力一样工作。对不起,我实际上并没有早点发布这个。

于 2013-03-01T00:21:39.263 回答