/// <summary> /// Shows the context menu in the preferred placement relative to the specified selection. /// </summary> /// <param name="selection">The coordinates (in DIPs) of the selected rectangle, relative to the window.</param> /// <param name="preferredPlacement">The preferred placement of the context menu relative to the selection rectangle.</param> /// <returns></returns> public Task<IUICommand> ShowForSelectionAsync(Rect selection, Placement preferredPlacement) { if (Commands.Count > MaxCommands) { throw new InvalidOperationException(); } #if WINDOWS_UWP return Task.Run<IUICommand>(async () => { foreach (IUICommand command in Commands) { _menu.Commands.Add(new Windows.UI.Popups.UICommand(command.Label, new Windows.UI.Popups.UICommandInvokedHandler((c2) => { command.Invoked?.Invoke(command); }), command.Id)); } Windows.Foundation.Rect r = new Windows.Foundation.Rect(selection.X, selection.Y, selection.Width, selection.Height); var c = await _menu.ShowForSelectionAsync(r, (Windows.UI.Popups.Placement)((int)preferredPlacement)); return c == null ? null : new UICommand(c.Label, new UICommandInvokedHandler((c2) => { c2.Invoked?.Invoke(c2); }), c.Id); }); #elif __ANDROID__ Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity); Android.App.AlertDialog dialog = builder.Create(); dialog.SetTitle(Title); dialog.SetMessage(Content); if (Commands.Count == 0) { dialog.SetButton(-1, Resources.System.GetString(Android.Resource.String.Cancel), new EventHandler<Android.Content.DialogClickEventArgs>(Clicked)); } else { for (int i = 0; i < Commands.Count; i++) { dialog.SetButton(-1 - i, Commands[i].Label, new EventHandler<Android.Content.DialogClickEventArgs>(Clicked)); } } dialog.Show(); return Task.Run<IUICommand>(() => { handle.WaitOne(); return _selectedCommand; }); #elif __IOS__ uac = UIAlertController.Create("", "", UIAlertControllerStyle.ActionSheet); if (Commands.Count == 0) { uac.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel | UIAlertActionStyle.Default, ActionClicked)); } else { for (int i = 0; i < Commands.Count; i++) { UIAlertAction action = UIAlertAction.Create(Commands[i].Label, UIAlertActionStyle.Default, ActionClicked); uac.AddAction(action); } } UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController; while (currentController.PresentedViewController != null) currentController = currentController.PresentedViewController; // set layout requirements for iPad var popoverController = uac.PopoverPresentationController; if(popoverController != null) { popoverController.SourceView = currentController.View; popoverController.SourceRect = new CoreGraphics.CGRect(selection.X, selection.Y, selection.Width, selection.Height); popoverController.PermittedArrowDirections = PlacementHelper.ToArrowDirection(preferredPlacement); } currentController.PresentViewController(uac, true, null); return Task.Run<IUICommand>(() => { handle.WaitOne(); return _selectedCommand; }); #else throw new PlatformNotSupportedException(); #endif }
/// <summary> /// Shows the context menu by the specified selection. /// </summary> /// <param name="selection">The coordinates (in DIPs) of the selected rectangle, relative to the window.</param> /// <returns></returns> public Task<IUICommand> ShowForSelectionAsync(Rect selection) { #if WINDOWS_UWP return Task.Run<IUICommand>(async () => { foreach (IUICommand command in Commands) { _menu.Commands.Add(new Windows.UI.Popups.UICommand(command.Label, new Windows.UI.Popups.UICommandInvokedHandler((c2) => { command.Invoked?.Invoke(command); }), command.Id)); } Windows.Foundation.Rect r = new Windows.Foundation.Rect(selection.X, selection.Y, selection.Width, selection.Height); var c = await _menu.ShowForSelectionAsync(r); return c == null ? null : new UICommand(c.Label, new UICommandInvokedHandler((c2) => { c2.Invoked?.Invoke(c2); }), c.Id); }); #else return ShowForSelectionAsync(selection, Placement.Default); #endif }