MobileToolItem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Windows.Input;
  4. using Xamarin.Forms;
  5. namespace InABox.Mobile
  6. {
  7. public interface IMobileToolItem
  8. {
  9. string Text { get; set; }
  10. ImageSource Image { get; set; }
  11. string Alert { get; set; }
  12. bool IsEnabled { get; set; }
  13. bool IsVisible { get; set; }
  14. int ID { get; set; }
  15. event EventHandler Clicked;
  16. void DoTap();
  17. int Row { get; set; }
  18. int Column { get; set; }
  19. }
  20. public class MobileToolItem : BindableObject, IMobileToolItem
  21. {
  22. public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(MobileToolItem), "");
  23. public string Text
  24. {
  25. get => (string)GetValue(TextProperty);
  26. set => SetValue(TextProperty, value);
  27. }
  28. public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(ImageSource), typeof(MobileToolItem), null);
  29. public ImageSource Image
  30. {
  31. get => (ImageSource)GetValue(ImageProperty);
  32. set =>SetValue(ImageProperty, value);
  33. }
  34. public static readonly BindableProperty AlertProperty = BindableProperty.Create(
  35. nameof(Alert),
  36. typeof(string),
  37. typeof(MobileToolItem),
  38. null);
  39. public string Alert
  40. {
  41. get => (string)GetValue(AlertProperty);
  42. set
  43. {
  44. SetValue(AlertProperty, value);
  45. OnPropertyChanged(nameof(AlertVisible));
  46. }
  47. }
  48. public bool AlertVisible => !String.IsNullOrWhiteSpace(Alert);
  49. public static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
  50. nameof(BackgroundColor),
  51. typeof(Color),
  52. typeof(MobileToolItem),
  53. Color.White);
  54. public Color BackgroundColor
  55. {
  56. get => (Color)GetValue(BackgroundColorProperty);
  57. set => SetValue(BackgroundColorProperty, value);
  58. }
  59. public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  60. nameof(TextColor),
  61. typeof(Color),
  62. typeof(MobileToolItem),
  63. Color.Black);
  64. public Color TextColor
  65. {
  66. get => (Color)GetValue(TextColorProperty);
  67. set => SetValue(TextColorProperty, value);
  68. }
  69. public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(MobileToolItem), true);
  70. public bool IsEnabled
  71. {
  72. get { return (bool)GetValue(IsEnabledProperty); }
  73. set
  74. {
  75. var old = (bool)GetValue(IsEnabledProperty);
  76. SetValue(IsEnabledProperty, value);
  77. if (old != value)
  78. DoEnabledChanged();
  79. }
  80. }
  81. public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(MobileToolItem), true);
  82. public bool IsVisible
  83. {
  84. get { return (bool)GetValue(IsVisibleProperty); }
  85. set
  86. {
  87. var old = (bool)GetValue(IsVisibleProperty);
  88. SetValue(IsVisibleProperty, value);
  89. if (old != value)
  90. DoVisibleChanged();
  91. }
  92. }
  93. public static readonly BindableProperty RowProperty = BindableProperty.Create(nameof(Row), typeof(Int32), typeof(MobileToolItem));
  94. public Int32 Row
  95. {
  96. get => (Int32)GetValue(RowProperty);
  97. set => SetValue(RowProperty, value);
  98. }
  99. public static readonly BindableProperty ColumnProperty = BindableProperty.Create(nameof(Column), typeof(Int32), typeof(MobileToolItem));
  100. public Int32 Column
  101. {
  102. get => (Int32)GetValue(ColumnProperty);
  103. set => SetValue(ColumnProperty, value);
  104. }
  105. public int ID { get; set; }
  106. public event EventHandler Clicked;
  107. public void DoTap()
  108. {
  109. Clicked?.Invoke(this, EventArgs.Empty);
  110. }
  111. public event EventHandler VisibleChanged;
  112. public void DoVisibleChanged()
  113. {
  114. VisibleChanged?.Invoke(this, EventArgs.Empty);
  115. }
  116. public event EventHandler EnabledChanged;
  117. public void DoEnabledChanged()
  118. {
  119. EnabledChanged?.Invoke(this, EventArgs.Empty);
  120. }
  121. }
  122. }