MobileView.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using Syncfusion.XForms.PopupLayout;
  3. using Xamarin.Forms;
  4. namespace InABox.Mobile
  5. {
  6. public abstract class MobileView<TViewModel> : BaseMobileView
  7. where TViewModel : class, IMobileViewModel
  8. {
  9. private static readonly BindableProperty ViewModelProperty =
  10. BindableProperty.Create(
  11. nameof(ViewModel),
  12. typeof(TViewModel),
  13. typeof(MobileView<TViewModel>),
  14. null
  15. );
  16. public TViewModel ViewModel
  17. {
  18. get => (TViewModel)GetValue(ViewModelProperty);
  19. set
  20. {
  21. if (ViewModel != null)
  22. ViewModel.Loaded -= ViewModel_OnLoaded;
  23. SetValue(ViewModelProperty,value);
  24. if (ViewModel != null)
  25. ViewModel.Loaded += ViewModel_OnLoaded;
  26. }
  27. }
  28. private bool _loaded = false;
  29. protected override void DoBindingContextChanged()
  30. {
  31. ViewModel = BindingContext as TViewModel;
  32. Refresh();
  33. _loaded = ViewModel != null;
  34. }
  35. private void ViewModel_OnLoaded(object sender, MobileViewModelLoadedEventArgs args)
  36. {
  37. Refresh();
  38. }
  39. public abstract void Refresh();
  40. public event MobileViewChangedEvent Changed;
  41. protected void DoChanged(string property)
  42. {
  43. if (_loaded)
  44. Changed?.Invoke(this, new MobileViewChangedEventArgs(property));
  45. }
  46. private readonly SfPopupLayout _popup = new SfPopupLayout();
  47. protected void ShowPopup(Func<View> view, int height = -1, int width = -1, int padding = 10)
  48. {
  49. _popup.PopupView.HeightRequest = height == -1 ? Application.Current.MainPage.Height * 0.8 : height;
  50. _popup.PopupView.WidthRequest = width == -1 ? Application.Current.MainPage.Width * 0.8 : width;
  51. _popup.PopupView.ShowHeader = false;
  52. _popup.PopupView.ShowFooter = false;
  53. _popup.PopupView.ContentTemplate = new DataTemplate(() =>
  54. {
  55. Grid grid = new Grid() { Margin = padding, Padding = padding};
  56. grid.Children.Add(view());
  57. return grid;
  58. });
  59. _popup.Show();
  60. }
  61. protected void DismissPopup()
  62. {
  63. _popup.Dismiss();
  64. }
  65. }
  66. }