| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using Syncfusion.XForms.PopupLayout;
- using Xamarin.Forms;
- namespace InABox.Mobile
- {
- public interface IPopupManager
- {
- bool Initialise();
-
- }
- public class PopupManagerConfiguration
- {
- public int RequestedHeight { get; set; } = 700;
- public int RequestedWidth { get; set; } = 350;
- public bool ShowHeader { get; set; } = false;
- public bool ShowFooter { get; set; } = false;
- public int Padding { get; set; } = 10;
- public int CornerRadius { get; set; } = 5;
- public bool Modal { get; set; } = false;
- }
-
- public static class PopupManager
- {
-
- private static SfPopupLayout _popup = null;
-
- private static ContentPage FindParentPage(Element view)
- {
- if (view is ContentPage page)
- return page;
- return FindParentPage(view.Parent);
- }
-
- public static void DisplayError(Element view, string message)
- {
- MobileLogging.Log("Unable to create Popup Layout!");
- var page = FindParentPage(view);
- Device.BeginInvokeOnMainThread(() =>
- {
- page.DisplayAlert("Error", message, "OK");
- });
- }
- public static void ShowPopup(Element parent, Func<View> view, PopupManagerConfiguration config = null)
- {
- if (_popup == null)
- {
- var service = DependencyService.Get<IPopupManager>();
- if (service.Initialise())
- _popup = new SfPopupLayout();
- }
- if (_popup == null)
- {
- DisplayError(parent, "Unable to create Popup!\nPlease try again or restart the application.");
- return;
- }
- var cfg = config ?? new PopupManagerConfiguration();
-
- _popup.PopupView.HeightRequest = cfg.RequestedHeight;
- _popup.PopupView.WidthRequest = cfg.RequestedWidth;
- _popup.PopupView.ShowHeader = cfg.ShowHeader;
- _popup.PopupView.ShowFooter = cfg.ShowFooter;
- _popup.PopupView.PopupStyle.CornerRadius = cfg.CornerRadius;
- _popup.StaysOpen = cfg.Modal;
-
- _popup.PopupView.ContentTemplate = new DataTemplate(() =>
- {
- Grid grid = new Grid() { Padding = cfg.Padding };
- grid.Children.Add(view());
- return grid;
- });
-
- _popup.Show();
- }
- public static void DismissPopup()
- {
- if (_popup != null)
- _popup.Dismiss();
- }
- }
- }
|