DateButton.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Globalization;
  3. using InABox.Core;
  4. using Syncfusion.XForms.PopupLayout;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Xaml;
  7. using XF.Material.Forms.Resources.Typography;
  8. namespace PRS.Mobile
  9. {
  10. public class DateButtonChangedArgs : EventArgs
  11. {
  12. public DateTime Date { get; private set; }
  13. public DateButtonChangedArgs(DateTime date)
  14. {
  15. Date = date;
  16. }
  17. }
  18. public delegate void DateButtonChanged(object sender, DateButtonChangedArgs args);
  19. public class DateTimeFormatter : IValueConverter
  20. {
  21. public String Prefix { get; set; }
  22. public String Format { get; set; }
  23. public String Prompt { get; set; }
  24. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  25. {
  26. if ((value is DateTime date) && (!date.IsEmpty()))
  27. {
  28. String fmt = "{0} {1:" + Format + "}";
  29. return String.Format(fmt,Prefix,date).Trim();
  30. }
  31. return Prompt;
  32. }
  33. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. public DateTimeFormatter()
  38. {
  39. Prompt = "Select Date:";
  40. Format = "dd MMMM yy";
  41. Prefix = "";
  42. }
  43. }
  44. [XamlCompilation(XamlCompilationOptions.Compile)]
  45. public partial class DateButton
  46. {
  47. private SfPopupLayout popup;
  48. public event DateButtonChanged Changed;
  49. public static readonly BindableProperty PromptProperty = BindableProperty.Create(
  50. nameof(Prompt),
  51. typeof(String),
  52. typeof(DateButton));
  53. public String Prompt
  54. {
  55. get => (String)GetValue(PromptProperty);
  56. set
  57. {
  58. SetValue(PromptProperty, value);
  59. _datetimeformatter.Prompt = value;
  60. }
  61. }
  62. public static readonly BindableProperty PrefixProperty = BindableProperty.Create(
  63. nameof(Prefix),
  64. typeof(String),
  65. typeof(DateButton));
  66. public String Prefix
  67. {
  68. get => (String)GetValue(PrefixProperty);
  69. set
  70. {
  71. SetValue(PrefixProperty, value);
  72. _datetimeformatter.Prefix = value;
  73. }
  74. }
  75. public static readonly BindableProperty FormatProperty = BindableProperty.Create(
  76. nameof(Format),
  77. typeof(String),
  78. typeof(DateButton));
  79. public String Format
  80. {
  81. get => (String)GetValue(FormatProperty);
  82. set
  83. {
  84. SetValue(FormatProperty, value);
  85. _datetimeformatter.Format = value;
  86. }
  87. }
  88. public static readonly BindableProperty DateProperty = BindableProperty.Create(
  89. nameof(Date),
  90. typeof(DateTime),
  91. typeof(DateButton));
  92. public DateTime Date
  93. {
  94. get => (DateTime)GetValue(DateProperty);
  95. set => SetValue(DateProperty,value);
  96. }
  97. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  98. nameof(TextColor),
  99. typeof(Color),
  100. typeof(DateButton),
  101. XF.Material.Forms.Material.Color.OnSecondary);
  102. public Color TextColor
  103. {
  104. get => (Color)GetValue(TextColorProperty);
  105. set => SetValue(TextColorProperty, value);
  106. }
  107. public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  108. nameof(TypeScale),
  109. typeof(MaterialTypeScale),
  110. typeof(DateButton),
  111. MaterialTypeScale.Button);
  112. public MaterialTypeScale TypeScale
  113. {
  114. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  115. set => SetValue(TypeScaleProperty, value);
  116. }
  117. public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
  118. nameof(ButtonColor),
  119. typeof(Color),
  120. typeof(DateButton),
  121. XF.Material.Forms.Material.Color.Secondary);
  122. public Color ButtonColor
  123. {
  124. get => (Color)GetValue(ButtonColorProperty);
  125. set => SetValue(ButtonColorProperty, value);
  126. }
  127. public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  128. nameof(BorderColor),
  129. typeof(Color),
  130. typeof(DateButton),
  131. XF.Material.Forms.Material.Color.SecondaryVariant);
  132. public Color BorderColor
  133. {
  134. get => (Color)GetValue(BorderColorProperty);
  135. set => SetValue(BorderColorProperty, value);
  136. }
  137. // public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
  138. // nameof(Padding),
  139. // typeof(Thickness),
  140. // typeof(DateButton),
  141. // new Thickness(5));
  142. //
  143. // public new Thickness Padding
  144. // {
  145. // get => (Thickness)GetValue(PaddingProperty);
  146. // set => SetValue(PaddingProperty, value);
  147. // }
  148. public DateButton()
  149. {
  150. InitializeComponent();
  151. popup = new SfPopupLayout();
  152. }
  153. private void _frame_OnClicked(object sender, EventArgs e)
  154. {
  155. popup.PopupView.WidthRequest = 300;
  156. popup.PopupView.HeightRequest = 500;
  157. popup.PopupView.ShowHeader = false;
  158. popup.PopupView.ShowFooter = false;
  159. DateSelector popupContent = new DateSelector();
  160. popupContent.Date = Date;
  161. popupContent.DateChanged += (o, args) =>
  162. {
  163. Date = args.Date;
  164. Changed?.Invoke(this,new DateButtonChangedArgs(Date));
  165. };
  166. popupContent.Margin = new Thickness(10);
  167. popupContent.HorizontalOptions = LayoutOptions.Fill;
  168. popupContent.VerticalOptions = LayoutOptions.Fill;
  169. popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
  170. popup.Show();
  171. }
  172. }
  173. }