TimeButton.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Syncfusion.XForms.PopupLayout;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Xaml;
  10. using XF.Material.Forms.Resources.Typography;
  11. namespace comal.timesheets
  12. {
  13. public class TimeButtonChangedArgs : EventArgs
  14. {
  15. public TimeSpan Time { get; private set; }
  16. public TimeButtonChangedArgs(TimeSpan time)
  17. {
  18. Time = Time;
  19. }
  20. }
  21. public delegate void TimeButtonChanged(object sender, TimeButtonChangedArgs args);
  22. public class TimeSpanFormatter : IValueConverter
  23. {
  24. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  25. {
  26. if ((value is TimeSpan time) && (time != TimeSpan.Zero))
  27. return $"{DateTime.Today.Add(time):hh\\:mm tt}";
  28. return "--";
  29. }
  30. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. }
  35. [XamlCompilation(XamlCompilationOptions.Compile)]
  36. public partial class TimeButton
  37. {
  38. private SfPopupLayout popup;
  39. public event TimeButtonChanged Changed;
  40. public static readonly BindableProperty TimeProperty = BindableProperty.Create(
  41. nameof(Time),
  42. typeof(TimeSpan),
  43. typeof(TimeButton));
  44. public TimeSpan Time
  45. {
  46. get => (TimeSpan)GetValue(TimeProperty);
  47. set => SetValue(TimeProperty,value);
  48. }
  49. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  50. nameof(TextColor),
  51. typeof(Color),
  52. typeof(TimeButton),
  53. XF.Material.Forms.Material.Color.OnSecondary);
  54. public Color TextColor
  55. {
  56. get => (Color)GetValue(TextColorProperty);
  57. set => SetValue(TextColorProperty, value);
  58. }
  59. public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  60. nameof(TypeScale),
  61. typeof(MaterialTypeScale),
  62. typeof(TimeButton),
  63. MaterialTypeScale.Button);
  64. public MaterialTypeScale TypeScale
  65. {
  66. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  67. set => SetValue(TypeScaleProperty, value);
  68. }
  69. public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  70. nameof(ButtonColor),
  71. typeof(Color),
  72. typeof(TimeButton),
  73. XF.Material.Forms.Material.Color.Secondary);
  74. public Color ButtonColor
  75. {
  76. get => (Color)GetValue(ButtonColorProperty);
  77. set => SetValue(ButtonColorProperty, value);
  78. }
  79. public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  80. nameof(BorderColor),
  81. typeof(Color),
  82. typeof(TimeButton),
  83. XF.Material.Forms.Material.Color.SecondaryVariant);
  84. public Color BorderColor
  85. {
  86. get => (Color)GetValue(BorderColorProperty);
  87. set => SetValue(BorderColorProperty, value);
  88. }
  89. public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
  90. nameof(Padding),
  91. typeof(Thickness),
  92. typeof(TimeButton),
  93. new Thickness(5));
  94. public new Thickness Padding
  95. {
  96. get => (Thickness)GetValue(PaddingProperty);
  97. set => SetValue(PaddingProperty, value);
  98. }
  99. public TimeButton()
  100. {
  101. InitializeComponent();
  102. popup = new SfPopupLayout();
  103. }
  104. private void _frame_OnClicked(object sender, EventArgs e)
  105. {
  106. popup.PopupView.WidthRequest = 300;
  107. popup.PopupView.HeightRequest = 500;
  108. popup.PopupView.ShowHeader = false;
  109. popup.PopupView.ShowFooter = false;
  110. TimeSelector popupContent = new TimeSelector();
  111. popupContent.Time = Time;
  112. popupContent.TimeChanged += (o, args) =>
  113. {
  114. Time = args.Time;
  115. Changed?.Invoke(this,new TimeButtonChangedArgs(Time));
  116. };
  117. popupContent.Margin = new Thickness(10);
  118. popupContent.HorizontalOptions = LayoutOptions.Fill;
  119. popupContent.VerticalOptions = LayoutOptions.Fill;
  120. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  121. popup.Show();
  122. }
  123. }
  124. }