PopupManager.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using Syncfusion.XForms.PopupLayout;
  3. using Xamarin.Forms;
  4. namespace InABox.Mobile
  5. {
  6. public interface IPopupManager
  7. {
  8. bool Initialise();
  9. }
  10. public class PopupManagerConfiguration
  11. {
  12. public int RequestedHeight { get; set; } = 700;
  13. public int RequestedWidth { get; set; } = 350;
  14. public bool ShowHeader { get; set; } = false;
  15. public bool ShowFooter { get; set; } = false;
  16. public int Padding { get; set; } = 10;
  17. public int CornerRadius { get; set; } = 5;
  18. public bool Modal { get; set; } = false;
  19. }
  20. public static class PopupManager
  21. {
  22. private static SfPopupLayout _popup = null;
  23. private static ContentPage FindParentPage(Element view)
  24. {
  25. if (view is ContentPage page)
  26. return page;
  27. return FindParentPage(view.Parent);
  28. }
  29. public static void DisplayError(Element view, string message)
  30. {
  31. MobileLogging.Log("Unable to create Popup Layout!");
  32. var page = FindParentPage(view);
  33. Device.BeginInvokeOnMainThread(() =>
  34. {
  35. page.DisplayAlert("Error", message, "OK");
  36. });
  37. }
  38. public static void ShowPopup(Element parent, Func<View> view, PopupManagerConfiguration config = null)
  39. {
  40. if (_popup == null)
  41. {
  42. var service = DependencyService.Get<IPopupManager>();
  43. if (service.Initialise())
  44. _popup = new SfPopupLayout();
  45. }
  46. if (_popup == null)
  47. {
  48. DisplayError(parent, "Unable to create Popup!\nPlease try again or restart the application.");
  49. return;
  50. }
  51. var cfg = config ?? new PopupManagerConfiguration();
  52. _popup.PopupView.HeightRequest = cfg.RequestedHeight;
  53. _popup.PopupView.WidthRequest = cfg.RequestedWidth;
  54. _popup.PopupView.ShowHeader = cfg.ShowHeader;
  55. _popup.PopupView.ShowFooter = cfg.ShowFooter;
  56. _popup.PopupView.PopupStyle.CornerRadius = cfg.CornerRadius;
  57. _popup.StaysOpen = cfg.Modal;
  58. _popup.PopupView.ContentTemplate = new DataTemplate(() =>
  59. {
  60. Grid grid = new Grid() { Padding = cfg.Padding };
  61. grid.Children.Add(view());
  62. return grid;
  63. });
  64. _popup.Show();
  65. }
  66. public static void DismissPopup()
  67. {
  68. if (_popup != null)
  69. _popup.Dismiss();
  70. }
  71. }
  72. }