private Task <MediaFile> GetMediaAsync(UIImagePickerControllerSourceType sourceType, string mediaType, StoreCameraMediaOptions options = null, CancellationToken token = default(CancellationToken)) { UIViewController viewController = null; UIWindow window = UIApplication.SharedApplication.KeyWindow; if (window == null) { throw new InvalidOperationException("There's no current active window"); } if (window.WindowLevel == UIWindowLevel.Normal) { viewController = window.RootViewController; } if (viewController == null) { window = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal); if (window == null) { throw new InvalidOperationException("Could not find current view controller"); } else { viewController = window.RootViewController; } } while (viewController.PresentedViewController != null) { viewController = viewController.PresentedViewController; } if (token.IsCancellationRequested) { return(Task.FromResult((MediaFile)null)); } MediaPickerDelegate ndelegate = new MediaPickerDelegate(viewController, sourceType, options, token); var od = Interlocked.CompareExchange(ref pickerDelegate, ndelegate, null); if (od != null) { throw new InvalidOperationException("Only one operation can be active at a time"); } var picker = SetupController(ndelegate, sourceType, mediaType, options); if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad && sourceType == UIImagePickerControllerSourceType.PhotoLibrary) { ndelegate.Popover = popover = new UIPopoverController(picker); ndelegate.Popover.Delegate = new MediaPickerPopoverDelegate(ndelegate, picker); ndelegate.DisplayPopover(); token.Register(() => { if (popover == null) { return; } NSRunLoop.Main.BeginInvokeOnMainThread(() => { ndelegate.Popover.Dismiss(true); ndelegate.CancelTask(); }); }); } else { if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { picker.ModalPresentationStyle = options?.ModalPresentationStyle == MediaPickerModalPresentationStyle.OverFullScreen ? UIModalPresentationStyle.OverFullScreen : UIModalPresentationStyle.FullScreen; } viewController.PresentViewController(picker, true, null); token.Register(() => { if (picker == null) { return; } NSRunLoop.Main.BeginInvokeOnMainThread(() => { picker.DismissModalViewController(true); ndelegate.CancelTask(); }); }); } return(ndelegate.Task.ContinueWith(t => { try { popover?.Dispose(); } catch { } popover = null; Interlocked.Exchange(ref pickerDelegate, null); try { picker?.Dispose(); } catch { } picker = null; return t; }).Unwrap()); }
Task <MediaFile> GetMediaAsync(UIImagePickerControllerSourceType sourceType, string mediaType, StoreCameraMediaOptions options = null, CancellationToken token = default(CancellationToken)) { var viewController = GetHostViewController(); if (token.IsCancellationRequested) { return(Task.FromResult((MediaFile)null)); } var ndelegate = new MediaPickerDelegate(viewController, sourceType, options, token); var od = Interlocked.CompareExchange(ref pickerDelegate, ndelegate, null); if (od != null) { throw new InvalidOperationException("Only one operation can be active at a time"); } var tcs = new TaskCompletionSource <Task>(); var picker = SetupController(ndelegate, sourceType, mediaType, tcs, options); tcs.Task.ContinueWith(t => Dismiss(popover, picker)); if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad && sourceType == UIImagePickerControllerSourceType.PhotoLibrary) { ndelegate.Popover = popover = new UIPopoverController(picker); ndelegate.Popover.Delegate = new MediaPickerPopoverDelegate(ndelegate, picker); ndelegate.DisplayPopover(); token.Register(() => { if (popover == null) { return; } NSRunLoop.Main.BeginInvokeOnMainThread(() => { ndelegate.Popover.Dismiss(true); ndelegate.CancelTask(); }); }); } else { if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0)) { picker.ModalPresentationStyle = options?.ModalPresentationStyle == MediaPickerModalPresentationStyle.OverFullScreen ? UIModalPresentationStyle.OverFullScreen : UIModalPresentationStyle.FullScreen; } viewController.PresentViewController(picker, true, null); token.Register(() => { if (picker == null) { return; } NSRunLoop.Main.BeginInvokeOnMainThread(() => { picker.DismissModalViewController(true); ndelegate.CancelTask(); }); }); } return(ndelegate.Task.ContinueWith(t => t.Result == null ? null : t.Result.FirstOrDefault())); }