Inheritance: CNContactPickerDelegate
        public Task <ContactInfo> GetSelectedContactInfo()
        {
            var picker = new CNContactPickerViewController()
            {
                DisplayedPropertyKeys = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName }
            };

            // Set up Contact Picker Delegate, TaskCompletionSource wrapper.
            var tcs            = new TaskCompletionSource <ContactInfo>();
            var pickerDelegate = new ContactPickerDelegate();

            picker.Delegate = pickerDelegate;

            pickerDelegate.ContactSelected += (contact) =>
            {
                tcs.TrySetResult(new ContactInfo {
                    Id = contact.Identifier, FamilyName = contact.FamilyName, AdditionalName = contact.MiddleName, GivenName = contact.GivenName
                });
            };

            pickerDelegate.SelectionCanceled += () =>
            {
                tcs.TrySetResult(null);
            };

            // Display as modal dialog on current ViewController
            UIWindow         window         = UIApplication.SharedApplication.KeyWindow;
            UIViewController viewController = window.RootViewController;

            if (viewController == null)
            {
                while (viewController.PresentedViewController != null)
                {
                    viewController = viewController.PresentedViewController;
                }
            }

            viewController.PresentViewController(picker, true, null);

            return(tcs.Task);
        }
Esempio n. 2
0
        public Task<ContactInfo> GetSelectedContactInfo()
        {
            var picker = new CNContactPickerViewController()
            {
                DisplayedPropertyKeys = new NSString[] { CNContactKey.GivenName, CNContactKey.FamilyName }
            };
            
            // Set up Contact Picker Delegate, TaskCompletionSource wrapper.
            var tcs = new TaskCompletionSource<ContactInfo>();
            var pickerDelegate = new ContactPickerDelegate();
            picker.Delegate = pickerDelegate;

            pickerDelegate.ContactSelected += (contact) =>
            {
                tcs.TrySetResult(new ContactInfo { Id = contact.Identifier, FamilyName = contact.FamilyName, AdditionalName = contact.MiddleName, GivenName = contact.GivenName });
            };

            pickerDelegate.SelectionCanceled += () =>
            {
                tcs.TrySetResult(null);
            };

            // Display as modal dialog on current ViewController
            UIWindow window = UIApplication.SharedApplication.KeyWindow;
            UIViewController viewController = window.RootViewController;
            if (viewController == null)
            {
                while (viewController.PresentedViewController != null)
                    viewController = viewController.PresentedViewController;
            }

            viewController.PresentViewController(picker, true, null);

            return tcs.Task;
            
        }