MobileButton.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. [XamlCompilation(XamlCompilationOptions.Compile)]
  34. public partial class MobileButton
  35. {
  36. public event MobileButtonClickEvent Clicked;
  37. public object Prefix
  38. {
  39. get => _textformatter.Prefix;
  40. set => _textformatter.Prefix = value;
  41. }
  42. public String Prompt
  43. {
  44. get => _textformatter.Prompt;
  45. set => _textformatter.Prompt = value;
  46. }
  47. public static readonly BindableProperty TagProperty = BindableProperty.Create(
  48. nameof(Tag),
  49. typeof(object),
  50. typeof(MobileButton));
  51. public object Tag
  52. {
  53. get => GetValue(TagProperty);
  54. set => SetValue(TagProperty, value);
  55. }
  56. private static readonly BindableProperty OrientationProperty = BindableProperty.Create(
  57. nameof(Orientation),
  58. typeof(StackOrientation),
  59. typeof(MobileButton),
  60. StackOrientation.Horizontal);
  61. public StackOrientation Orientation
  62. {
  63. get => (StackOrientation)GetValue(OrientationProperty);
  64. set => SetValue(OrientationProperty, value);
  65. }
  66. public static readonly BindableProperty ImageProperty = BindableProperty.Create(
  67. nameof(Image),
  68. typeof(ImageSource),
  69. typeof(MobileButton)
  70. );
  71. public ImageSource Image
  72. {
  73. get => GetValue(ImageProperty) as ImageSource;
  74. set => SetValue(ImageProperty, value);
  75. }
  76. public static readonly BindableProperty TextProperty = BindableProperty.Create(
  77. nameof(Text),
  78. typeof(String),
  79. typeof(MobileButton)
  80. );
  81. public String Text
  82. {
  83. get => (String)GetValue(TextProperty);
  84. set => SetValue(TextProperty, value);
  85. }
  86. private static readonly BindableProperty AlertProperty = BindableProperty.Create(
  87. nameof(Alert),
  88. typeof(String),
  89. typeof(MobileButton)
  90. );
  91. public String Alert
  92. {
  93. get => (String)GetValue(AlertProperty);
  94. set => SetValue(AlertProperty, value);
  95. }
  96. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  97. nameof(TextColor),
  98. typeof(Color),
  99. typeof(MobileButton),
  100. XF.Material.Forms.Material.Color.OnSecondary);
  101. public Color TextColor
  102. {
  103. get => (Color)GetValue(TextColorProperty);
  104. set => SetValue(TextColorProperty, value);
  105. }
  106. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  107. nameof(TypeScale),
  108. typeof(MaterialTypeScale),
  109. typeof(MobileButton),
  110. MaterialTypeScale.Button);
  111. public MaterialTypeScale TypeScale
  112. {
  113. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  114. set => SetValue(TypeScaleProperty, value);
  115. }
  116. public new static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
  117. nameof(BackgroundColor),
  118. typeof(Color),
  119. typeof(MobileButton),
  120. XF.Material.Forms.Material.Color.Secondary);
  121. public new Color BackgroundColor
  122. {
  123. get => (Color)GetValue(BackgroundColorProperty);
  124. set => SetValue(BackgroundColorProperty, value);
  125. }
  126. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  127. nameof(BorderColor),
  128. typeof(Color),
  129. typeof(MobileButton),
  130. XF.Material.Forms.Material.Color.SecondaryVariant);
  131. public Color BorderColor
  132. {
  133. get => (Color)GetValue(BorderColorProperty);
  134. set => SetValue(BorderColorProperty, value);
  135. }
  136. public MobileButton()
  137. {
  138. InitializeComponent();
  139. Alert = "";
  140. HeightRequest = 40;
  141. }
  142. private void _frame_OnClicked(object sender, EventArgs e)
  143. {
  144. Clicked?.Invoke(this, new MobileButtonClickEventArgs(Tag));
  145. }
  146. }
  147. }