public void ShowDialog(string name, IDialogParameters parameters, Action <IDialogResult> callback) { try { parameters = UriParsingHelper.GetSegmentParameters(name, parameters); var view = CreateViewFor(UriParsingHelper.GetSegmentName(name)); var dialogAware = InitializeDialog(view, parameters); var currentPage = GetCurrentContentPage(); var dialogModal = new DialogPage(); dialogAware.RequestClose += DialogAware_RequestClose; void DialogAware_RequestClose(IDialogParameters outParameters) { try { var result = CloseDialog(outParameters ?? new DialogParameters(), currentPage, dialogModal); if (result.Exception is DialogException de && de.Message == DialogException.CanCloseIsFalse) { return; } dialogAware.RequestClose -= DialogAware_RequestClose; callback?.Invoke(result); GC.Collect(); } catch (DialogException dex) { if (dex.Message != DialogException.CanCloseIsFalse) { callback?.Invoke(new DialogResult { Exception = dex, Parameters = parameters }); } } catch (Exception ex) { callback?.Invoke(new DialogResult { Exception = ex, Parameters = parameters }); } } if (!parameters.TryGetValue <bool>(KnownDialogParameters.CloseOnBackgroundTapped, out var closeOnBackgroundTapped)) { var dialogLayoutCloseOnBackgroundTapped = DialogLayout.GetCloseOnBackgroundTapped(view); if (dialogLayoutCloseOnBackgroundTapped.HasValue) { closeOnBackgroundTapped = dialogLayoutCloseOnBackgroundTapped.Value; } } InsertPopupViewInCurrentPage(currentPage as ContentPage, dialogModal, view, closeOnBackgroundTapped, DialogAware_RequestClose); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(currentPage, aa => aa.IsActive = false); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(view, aa => aa.IsActive = true); } catch (Exception ex) { var error = ex.ToString(); callback?.Invoke(new DialogResult { Exception = ex }); } }
private IDialogResult CloseDialog(IDialogParameters parameters, ContentPage currentPage, DialogPage dialogModal) { try { if (parameters is null) { parameters = new DialogParameters(); } var view = dialogModal.DialogView; var dialogAware = GetDialogController(view); if (!dialogAware.CanCloseDialog()) { throw new DialogException(DialogException.CanCloseIsFalse); } currentPage.Navigation.PopModalAsync(true); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(view, aa => aa.IsActive = false); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(currentPage, aa => aa.IsActive = true); dialogAware.OnDialogClosed(); return(new DialogResult { Parameters = parameters }); } catch (DialogException) { throw; } catch (Exception ex) { return(new DialogResult { Exception = ex, Parameters = parameters }); } }
private void InsertPopupViewInCurrentPage(ContentPage currentPage, DialogPage modalPage, View popupView, bool hideOnBackgroundTapped, Action <IDialogParameters> callback) { View mask = DialogLayout.GetMask(popupView); if (mask is null) { Style overlayStyle = GetOverlayStyle(popupView); mask = new BoxView { Style = overlayStyle }; } mask.SetBinding(VisualElement.WidthRequestProperty, new Binding { Path = "Width", Source = modalPage }); mask.SetBinding(VisualElement.HeightRequestProperty, new Binding { Path = "Height", Source = modalPage }); if (hideOnBackgroundTapped) { var dismissCommand = new Command(() => callback(new DialogParameters())); mask.GestureRecognizers.Add(new TapGestureRecognizer { Command = dismissCommand }); } var overlay = new AbsoluteLayout(); var popupContainer = new DialogContainer { IsPopupContent = true, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, Content = popupView, }; var relativeWidth = DialogLayout.GetRelativeWidthRequest(popupView); if (relativeWidth != null) { popupContainer.SetBinding(DialogContainer.WidthRequestProperty, new Binding("Width", BindingMode.OneWay, new RelativeContentSizeConverter { RelativeSize = relativeWidth.Value }, source: modalPage)); } var relativeHeight = DialogLayout.GetRelativeHeightRequest(popupView); if (relativeHeight != null) { popupContainer.SetBinding(DialogContainer.HeightRequestProperty, new Binding("Height", BindingMode.OneWay, new RelativeContentSizeConverter { RelativeSize = relativeHeight.Value }, source: modalPage)); } AbsoluteLayout.SetLayoutFlags(popupContainer, AbsoluteLayoutFlags.PositionProportional); var popupBounds = DialogLayout.GetLayoutBounds(popupView); AbsoluteLayout.SetLayoutBounds(popupContainer, popupBounds); if (DialogLayout.GetUseMask(popupContainer.Content) ?? true) { overlay.Children.Add(mask); } overlay.Children.Add(popupContainer); modalPage.Content = overlay; modalPage.DialogView = popupView; currentPage.Navigation.PushModalAsync(modalPage, true); }
private async System.Threading.Tasks.Task <IDialogResult> CloseDialogAsync(IDialogParameters parameters, ContentPage currentPage, DialogPage dialogModal) { try { PageNavigationService.NavigationSource = PageNavigationSource.DialogService; if (parameters is null) { parameters = new DialogParameters(); } var view = dialogModal.DialogView; var dialogAware = GetDialogController(view); if (!dialogAware.CanCloseDialog()) { throw new DialogException(DialogException.CanCloseIsFalse); } await currentPage.Navigation.PopModalAsync(true); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(view, aa => aa.IsActive = false); PageUtilities.InvokeViewAndViewModelAction <IActiveAware>(currentPage, aa => aa.IsActive = true); dialogAware.OnDialogClosed(); return(new DialogResult { Parameters = parameters }); } catch (DialogException) { throw; } catch (Exception ex) { return(new DialogResult { Exception = ex, Parameters = parameters }); } finally { PageNavigationService.NavigationSource = PageNavigationSource.Device; } }