public void ShowDialog(string name, IDialogParameters parameters, Action <IDialogResult> callback) { try { parameters = UriParsingHelper.GetSegmentParameters(name, parameters); var view = CreateViewFor(UriParsingHelper.GetSegmentName(name)); var popupPage = CreatePopupPageForView(view); var dialogAware = InitializeDialog(view, parameters); if (!parameters.TryGetValue <bool>(KnownDialogParameters.CloseOnBackgroundTapped, out var closeOnBackgroundTapped)) { var dialogLayoutCloseOnBackgroundTapped = DialogLayout.GetCloseOnBackgroundTapped(view); if (dialogLayoutCloseOnBackgroundTapped.HasValue) { closeOnBackgroundTapped = dialogLayoutCloseOnBackgroundTapped.Value; } } if (!parameters.TryGetValue <bool>(KnownPopupDialogParameters.Animated, out var animated)) { animated = true; } var popupDialogLayoutIsAnimationEnabled = PopupDialogLayout.GetIsAnimationEnabled(view); popupPage.IsAnimationEnabled = popupDialogLayoutIsAnimationEnabled ?? true; dialogAware.RequestClose += DialogAware_RequestClose; void CloseOnBackgroundClicked(object sender, EventArgs args) { DialogAware_RequestClose(new DialogParameters()); } void DialogAware_RequestClose(IDialogParameters outParameters) { try { var result = CloseDialog(outParameters ?? new DialogParameters(), popupPage, view); if (result.Exception is DialogException de && de.Message == DialogException.CanCloseIsFalse) { return; } dialogAware.RequestClose -= DialogAware_RequestClose; if (closeOnBackgroundTapped) { popupPage.BackgroundClicked -= CloseOnBackgroundClicked; } 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 (closeOnBackgroundTapped) { popupPage.BackgroundClicked += CloseOnBackgroundClicked; } PushPopupPage(popupPage, view, animated); } catch (Exception ex) { callback?.Invoke(new DialogResult { Exception = ex }); } }
private async void PushPopupPage(PopupPage popupPage, View dialogView, bool animated = true) { View mask = DialogLayout.GetMask(dialogView); if (mask is null) { Style overlayStyle = GetOverlayStyle(dialogView); mask = new BoxView { Style = overlayStyle }; } mask.SetBinding(VisualElement.WidthRequestProperty, new Binding { Path = "Width", Source = popupPage }); mask.SetBinding(VisualElement.HeightRequestProperty, new Binding { Path = "Height", Source = popupPage }); var overlay = new AbsoluteLayout(); var relativeWidth = DialogLayout.GetRelativeWidthRequest(dialogView); if (relativeWidth != null) { dialogView.SetBinding(View.WidthRequestProperty, new Binding("Width", BindingMode.OneWay, new RelativeContentSizeConverter { RelativeSize = relativeWidth.Value }, source: popupPage)); } var relativeHeight = DialogLayout.GetRelativeHeightRequest(dialogView); if (relativeHeight != null) { dialogView.SetBinding(View.HeightRequestProperty, new Binding("Height", BindingMode.OneWay, new RelativeContentSizeConverter { RelativeSize = relativeHeight.Value }, source: popupPage)); } //AbsoluteLayout.SetLayoutFlags(content, AbsoluteLayoutFlags.PositionProportional); //AbsoluteLayout.SetLayoutBounds(content, new Rectangle(0f, 0f, popupPage.Width, popupPage.Height)); AbsoluteLayout.SetLayoutFlags(dialogView, AbsoluteLayoutFlags.PositionProportional); var popupBounds = DialogLayout.GetLayoutBounds(dialogView); AbsoluteLayout.SetLayoutBounds(dialogView, popupBounds); //overlay.Children.Add(content); if (DialogLayout.GetUseMask(dialogView) ?? true) { overlay.Children.Add(mask); } overlay.Children.Add(dialogView); popupPage.Content = overlay; await _popupNavigation.PushAsync(popupPage, animated); }
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); }
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 void InsertPopupViewInCurrentPage(ContentPage currentPage, 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 = currentPage }); mask.SetBinding(VisualElement.HeightRequestProperty, new Binding { Path = "Height", Source = currentPage }); if (hideOnBackgroundTapped) { var dismissCommand = new Command(() => callback(new DialogParameters())); mask.GestureRecognizers.Add(new TapGestureRecognizer { Command = dismissCommand }); } var overlay = new AbsoluteLayout(); overlay.SetValue(IsPopupHostProperty, true); var existingContent = currentPage.Content; existingContent.BindingContext = currentPage.BindingContext; var content = new DialogContainer { Padding = currentPage.Padding, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, IsPageContent = true }; currentPage.Padding = new Thickness(0); 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: currentPage)); } var relativeHeight = DialogLayout.GetRelativeHeightRequest(popupView); if (relativeHeight != null) { popupContainer.SetBinding(DialogContainer.HeightRequestProperty, new Binding("Height", BindingMode.OneWay, new RelativeContentSizeConverter { RelativeSize = relativeHeight.Value }, source: currentPage)); } AbsoluteLayout.SetLayoutFlags(content, AbsoluteLayoutFlags.PositionProportional); AbsoluteLayout.SetLayoutBounds(content, new Rectangle(0f, 0f, currentPage.Width, currentPage.Height)); AbsoluteLayout.SetLayoutFlags(popupContainer, AbsoluteLayoutFlags.PositionProportional); var popupBounds = DialogLayout.GetLayoutBounds(popupView); AbsoluteLayout.SetLayoutBounds(popupContainer, popupBounds); overlay.Children.Add(content); if (DialogLayout.GetUseMask(popupContainer.Content) ?? true) { overlay.Children.Add(mask); } overlay.Children.Add(popupContainer); currentPage.Content = overlay; // The original content needs to be reparented after the Page Content has been reset. content.Content = existingContent; }