MobileButton.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Globalization;
  3. using InABox.Core;
  4. using Xamarin.Forms;
  5. using Xamarin.Forms.Xaml;
  6. using XF.Material.Forms.Resources.Typography;
  7. namespace InABox.Mobile
  8. {
  9. public class MobileButtonClickEventArgs : EventArgs
  10. {
  11. public object Tag { get; private set; }
  12. public MobileButtonClickEventArgs(object tag)
  13. {
  14. Tag = tag;
  15. }
  16. }
  17. public delegate void MobileButtonClickEvent(object sender, MobileButtonClickEventArgs args);
  18. public class MobileButtonTextFormatter : IValueConverter
  19. {
  20. public object Prefix { get; set; }
  21. public String Prompt { get; set; }
  22. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  23. {
  24. if (String.IsNullOrWhiteSpace(value as String))
  25. return $"[{Prompt}]";
  26. return $"{Prefix} {value}".Trim();
  27. }
  28. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. }
  33. public enum MobileButtonSizeDimension
  34. {
  35. Height,
  36. Width
  37. }
  38. public class MobileButtonSizeConverter : UtilityConverter<Size,double>
  39. {
  40. public MobileButtonSizeDimension Dimension { get; set; }
  41. protected override double Convert(Size value)
  42. {
  43. return Dimension == MobileButtonSizeDimension.Height
  44. ? value.Height
  45. : value.Width;
  46. }
  47. }
  48. [XamlCompilation(XamlCompilationOptions.Compile)]
  49. public partial class MobileButton
  50. {
  51. public event MobileButtonClickEvent Clicked;
  52. public object Prefix
  53. {
  54. get => _textformatter.Prefix;
  55. set => _textformatter.Prefix = value;
  56. }
  57. public String Prompt
  58. {
  59. get => _textformatter.Prompt;
  60. set => _textformatter.Prompt = value;
  61. }
  62. public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(
  63. nameof(CornerRadius),
  64. typeof(float),
  65. typeof(MobileButton));
  66. public float CornerRadius
  67. {
  68. get => (float)GetValue(CornerRadiusProperty);
  69. set => SetValue(CornerRadiusProperty, value);
  70. }
  71. public static readonly BindableProperty TagProperty = BindableProperty.Create(
  72. nameof(Tag),
  73. typeof(object),
  74. typeof(MobileButton));
  75. public object Tag
  76. {
  77. get => GetValue(TagProperty);
  78. set => SetValue(TagProperty, value);
  79. }
  80. private static readonly BindableProperty OrientationProperty = BindableProperty.Create(
  81. nameof(Orientation),
  82. typeof(StackOrientation),
  83. typeof(MobileButton),
  84. StackOrientation.Horizontal);
  85. public StackOrientation Orientation
  86. {
  87. get => (StackOrientation)GetValue(OrientationProperty);
  88. set => SetValue(OrientationProperty, value);
  89. }
  90. public static readonly BindableProperty ImageProperty = BindableProperty.Create(
  91. nameof(Image),
  92. typeof(ImageSource),
  93. typeof(MobileButton)
  94. );
  95. public ImageSource Image
  96. {
  97. get => GetValue(ImageProperty) as ImageSource;
  98. set => SetValue(ImageProperty, value);
  99. }
  100. public static readonly BindableProperty ImageSizeProperty = BindableProperty.Create(
  101. nameof(ImageSize),
  102. typeof(Size),
  103. typeof(MobileButton),
  104. new Size(24,24)
  105. );
  106. public Size ImageSize
  107. {
  108. get => (Size)GetValue(ImageSizeProperty);
  109. set => SetValue(ImageSizeProperty, value);
  110. }
  111. public static readonly BindableProperty TextProperty = BindableProperty.Create(
  112. nameof(Text),
  113. typeof(String),
  114. typeof(MobileButton)
  115. );
  116. public String Text
  117. {
  118. get => (String)GetValue(TextProperty);
  119. set => SetValue(TextProperty, value);
  120. }
  121. private static readonly BindableProperty AlertProperty = BindableProperty.Create(
  122. nameof(Alert),
  123. typeof(String),
  124. typeof(MobileButton)
  125. );
  126. public String Alert
  127. {
  128. get => (String)GetValue(AlertProperty);
  129. set => SetValue(AlertProperty, value);
  130. }
  131. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  132. nameof(TextColor),
  133. typeof(Color),
  134. typeof(MobileButton),
  135. XF.Material.Forms.Material.Color.OnSecondary);
  136. public Color TextColor
  137. {
  138. get => (Color)GetValue(TextColorProperty);
  139. set => SetValue(TextColorProperty, value);
  140. }
  141. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  142. nameof(TypeScale),
  143. typeof(MaterialTypeScale),
  144. typeof(MobileButton),
  145. MaterialTypeScale.Button);
  146. public MaterialTypeScale TypeScale
  147. {
  148. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  149. set => SetValue(TypeScaleProperty, value);
  150. }
  151. public new static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
  152. nameof(BackgroundColor),
  153. typeof(Color),
  154. typeof(MobileButton),
  155. XF.Material.Forms.Material.Color.Secondary);
  156. public new Color BackgroundColor
  157. {
  158. get => (Color)GetValue(BackgroundColorProperty);
  159. set => SetValue(BackgroundColorProperty, value);
  160. }
  161. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  162. nameof(BorderColor),
  163. typeof(Color),
  164. typeof(MobileButton),
  165. XF.Material.Forms.Material.Color.SecondaryVariant);
  166. public Color BorderColor
  167. {
  168. get => (Color)GetValue(BorderColorProperty);
  169. set => SetValue(BorderColorProperty, value);
  170. }
  171. public MobileButton()
  172. {
  173. InitializeComponent();
  174. Alert = "";
  175. HeightRequest = 40;
  176. }
  177. private async void _frame_OnClicked(object sender, EventArgs e)
  178. {
  179. Clicked?.Invoke(this, new MobileButtonClickEventArgs(Tag));
  180. }
  181. }
  182. }