Xamarin组件包(第二波)


Xamarin组件包 Xamarin.ToolKit第二波

Xamarin.forms


Xamarin.ToolKit第二波
442477-20170224141558554-509054350.png-608.5kB

一 圆角按钮

xamarin.froms提供的标准button按钮设置了圆角和边框,都没有明显圆角样式,于是乎自己重写了渲染类。道理吧就是重写ButtonRenderer,以下代码是安卓的实现,ios也有实现且非常简单这里就不贴出来了。

  1. 1 public class CustomButtonRenderer : ButtonRenderer
  2. 2 {
  3. 3 protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
  4. 4 {
  5. 5 base.OnElementChanged(e);
  6. 6 if (e.OldElement == null)
  7. 7 {
  8. 8
  9. 9 var selected = new GradientDrawable();//创建drawable
  10. 10 var selectedColor = new MyGraphic.Color(Element.BackgroundColor.ToAndroid().ToArgb() + 50);
  11. 11 selected.SetColor(selectedColor);
  12. 12 selected.SetCornerRadius(Element.BorderRadius*10);
  13. 13 selected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid());
  14. 14
  15. 15 var unSelected = new GradientDrawable();//创建drawable
  16. 16 unSelected.SetColor(Element.BackgroundColor.ToAndroid());
  17. 17 unSelected.SetCornerRadius(Element.BorderRadius * 10);
  18. 18 unSelected.SetStroke((int)Element.BorderWidth, Element.BorderColor.ToAndroid());
  19. 19
  20. 20 var drawable = new StateListDrawable();
  21. 21 drawable.AddState(new int[] { MyAndroid.Resource.Attribute.StatePressed },
  22. 22 selected);
  23. 23 drawable.AddState(new int[] { -MyAndroid.Resource.Attribute.StatePressed },
  24. 24 unSelected);
  25. 25
  26. 26 Control.SetBackgroundDrawable(drawable);
  27. 27 }
  28. 28 }
  29. 29 }

二 带图标的输入框

xamarin.froms提供的标准Entry,在安卓里面显示出来很丑只有一个下边框,且没有什么可定义的样式设置,于是乎自己重写定义一个Entry,让他具有边框,边框颜色,还有一点圆角,也可以设置左边和右边的图标

下面同样是安卓的实现代码

1 IconEntry类

  1. 1 public class IconEntry : Entry
  2. 2 {
  3. 3 #region 静态属性
  4. 4 public static readonly BindableProperty DrawLeftProperty;
  5. 5 public static readonly BindableProperty DrawRightProperty;
  6. 6 public static readonly BindableProperty BorderColorProperty;
  7. 7 #endregion
  8. 8
  9. 9 #region 属性
  10. 10
  11. 11 /// <summary>
  12. 12 /// 左边图标文件
  13. 13 /// </summary>
  14. 14 public string DrawLeft
  15. 15 {
  16. 16 get { return this.GetValue(DrawLeftProperty).ToString(); }
  17. 17 set { SetValue(DrawLeftProperty, value); }
  18. 18 }
  19. 19
  20. 20 /// <summary>
  21. 21 /// 右边图标文件
  22. 22 /// </summary>
  23. 23 public string DrawRight
  24. 24 {
  25. 25 get { return this.GetValue(DrawRightProperty).ToString(); }
  26. 26 set { SetValue(DrawRightProperty, value); }
  27. 27 }
  28. 28
  29. 29 /// <summary>
  30. 30 /// 边框颜色
  31. 31 /// </summary>
  32. 32 public Color BorderColor
  33. 33 {
  34. 34 get { return (Color)GetValue(BorderColorProperty); }
  35. 35 set { SetValue(BorderColorProperty, value); }
  36. 36 }
  37. 37
  38. 38 #endregion
  39. 39
  40. 40 #region 构造
  41. 41 static IconEntry()
  42. 42 {
  43. 43 DrawLeftProperty = BindableProperty.Create<IconEntry, string>(
  44. 44 p => p.DrawLeft,
  45. 45 string.Empty,
  46. 46 BindingMode.OneWay,
  47. 47 propertyChanged: DrawLeftChangedHandler);
  48. 48
  49. 49 DrawRightProperty = BindableProperty.Create<IconEntry, string>(
  50. 50 p => p.DrawRight,
  51. 51 string.Empty,
  52. 52 BindingMode.OneWay,
  53. 53 propertyChanged: DrawRightChangedHandler);
  54. 54
  55. 55 BorderColorProperty = BindableProperty.Create<IconEntry, Color>(
  56. 56 p => p.BorderColor,
  57. 57 Color.Transparent,
  58. 58 BindingMode.OneWay,
  59. 59 propertyChanged: BorderColorChangedHandler);
  60. 60 }
  61. 61 #endregion
  62. 62
  63. 63 #region 方法
  64. 64
  65. 65 #region DrawLeft变化处理
  66. 66 private static void DrawLeftChangedHandler(BindableObject bindable,
  67. 67 string oldValue, string newValue)
  68. 68 {
  69. 69 if (!oldValue.Equals(newValue))
  70. 70 {
  71. 71 var pageHead = (IconEntry)bindable;
  72. 72 pageHead.DrawLeft = newValue;
  73. 73 }
  74. 74 }
  75. 75 #endregion
  76. 76
  77. 77 #region DrawRight变化处理
  78. 78 private static void DrawRightChangedHandler(BindableObject bindable,
  79. 79 string oldValue, string newValue)
  80. 80 {
  81. 81 if (!oldValue.Equals(newValue))
  82. 82 {
  83. 83 var pageHead = (IconEntry)bindable;
  84. 84 pageHead.DrawRight = newValue;
  85. 85 }
  86. 86 }
  87. 87 #endregion
  88. 88
  89. 89 #region BorderColor变化处理
  90. 90 private static void BorderColorChangedHandler(BindableObject bindable,
  91. 91 Color oldValue, Color newValue)
  92. 92 {
  93. 93 if (!oldValue.Equals(newValue))
  94. 94 {
  95. 95 var pageHead = (IconEntry)bindable;
  96. 96 pageHead.BorderColor = newValue;
  97. 97 }
  98. 98 }
  99. 99 #endregion
  100. 100
  101. 101 #endregion
  102. 102 }

2 IconEntryRenderer类

  1. 1 public class IconEntryRenderer : EntryRenderer
  2. 2 {
  3. 3 private Bitmap LeftBitmap
  4. 4 {
  5. 5 get
  6. 6 {
  7. 7 if (Element is IconEntry)
  8. 8 {
  9. 9 var logEntry = (Element as IconEntry);
  10. 10 if (!string.IsNullOrEmpty(logEntry.DrawLeft))
  11. 11 {
  12. 12 var resStr = logEntry.DrawLeft;
  13. 13 if (resStr.Contains(".png"))
  14. 14 {
  15. 15 resStr = resStr.Replace(".png", "");
  16. 16 }
  17. 17 var id = Context.Resources.GetIdentifier(resStr,
  18. 18 "drawable",
  19. 19 Context.PackageName);
  20. 20 return BitmapFactory.DecodeResource(Resources, id);
  21. 21 }
  22. 22 }
  23. 23 return null;
  24. 24 }
  25. 25 }
  26. 26
  27. 27 private Bitmap RightBitmap
  28. 28 {
  29. 29 get
  30. 30 {
  31. 31 if (Element is IconEntry)
  32. 32 {
  33. 33 var logEntry = (Element as IconEntry);
  34. 34 if (!string.IsNullOrEmpty(logEntry.DrawRight))
  35. 35 {
  36. 36 var resStr = logEntry.DrawRight;
  37. 37 if (resStr.Contains(".png"))
  38. 38 {
  39. 39 resStr = resStr.Replace(".png", "");
  40. 40 }
  41. 41 var id = Context.Resources.GetIdentifier(resStr,
  42. 42 "drawable",
  43. 43 Context.PackageName);
  44. 44 return BitmapFactory.DecodeResource(Resources, id);
  45. 45 }
  46. 46 }
  47. 47 return null;
  48. 48 }
  49. 49 }
  50. 50
  51. 51 protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
  52. 52 {
  53. 53 base.OnElementChanged(e);
  54. 54 if (e.OldElement == null)
  55. 55 {
  56. 56
  57. 57 var unSelected = new GradientDrawable();//创建drawable
  58. 58 unSelected.SetCornerRadius(8);
  59. 59 unSelected.SetStroke(1, (Element as IconEntry).BorderColor.ToAndroid());
  60. 60 unSelected.SetShape(ShapeType.Rectangle);
  61. 61
  62. 62 var padLeft = 40;
  63. 63 if (LeftBitmap != null)
  64. 64 {
  65. 65 padLeft += LeftBitmap.Width;
  66. 66 }
  67. 67
  68. 68 var padRight = 20;
  69. 69 if (RightBitmap != null)
  70. 70 {
  71. 71 padRight += RightBitmap.Width;
  72. 72 }
  73. 73
  74. 74
  75. 75 Control.SetPadding(padLeft, 0, padRight, 0);
  76. 76 Control.SetTextColor(Element.TextColor.ToAndroid());
  77. 77 Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
  78. 78 Control.SetBackgroundDrawable(unSelected);
  79. 79 Control.Gravity = GravityFlags.CenterVertical;
  80. 80
  81. 81 }
  82. 82 }
  83. 83
  84. 84 protected override void OnDraw(Canvas canvas)
  85. 85 {
  86. 86 base.OnDraw(canvas);
  87. 87 if (LeftBitmap != null)
  88. 88 {
  89. 89 canvas.DrawBitmap(LeftBitmap, 20f, (Height - LeftBitmap.Height) / 2f, new Paint());
  90. 90 }
  91. 91
  92. 92 if (RightBitmap != null)
  93. 93 {
  94. 94 canvas.DrawBitmap(RightBitmap, Width-RightBitmap.Width - 20f, (Height - RightBitmap.Height) / 2f, new Paint());
  95. 95 }
  96. 96 }
  97. 97
  98. 98 }

三 同样如有朋友喜欢也可以付费支持下

1)支付宝:
442477-20170221123417413-1755987272.png-44.7kB 2) 微信:

442477-20170221122905116-1194918574.png-54kB

作者: bing
原文地址:http://www.cnblogs.com/rjjs/p/6438338.html

分享到