MobileView.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. protected override void DoBindingContextChanged()
  29. {
  30. ViewModel = BindingContext as TViewModel;
  31. Refresh();
  32. }
  33. private void ViewModel_OnLoaded(object sender, MobileViewModelLoadedEventArgs args)
  34. {
  35. Refresh();
  36. }
  37. public abstract void Refresh();
  38. public event MobileViewChangedEvent Changed;
  39. protected void DoChanged(string property)
  40. => Changed?.Invoke(this, new MobileViewChangedEventArgs(property));
  41. private readonly SfPopupLayout _popup = new SfPopupLayout();
  42. protected void ShowPopup(Func<View> view, int height = 500, int width = 300, int padding = 10)
  43. {
  44. _popup.PopupView.HeightRequest = height;
  45. _popup.PopupView.WidthRequest = width;
  46. _popup.PopupView.ShowHeader = false;
  47. _popup.PopupView.ShowFooter = false;
  48. _popup.PopupView.ContentTemplate = new DataTemplate(() =>
  49. {
  50. Grid grid = new Grid() { Margin = padding, Padding = padding};
  51. grid.Children.Add(view());
  52. return grid;
  53. });
  54. _popup.Show();
  55. }
  56. protected void DismissPopup()
  57. {
  58. _popup.Dismiss();
  59. }
  60. }
  61. }