CustomHubTileViewRenderer.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Media;
  4. using System.Windows.Media.Imaging;
  5. using FormulaOneApp.Controls.HubTileAbstraction;
  6. using FormulaOneApp.WinPhone.Controls;
  7. using Microsoft.Phone.Controls;
  8. using Xamarin.Forms;
  9. using Xamarin.Forms.Platform.WinPhone;
  10. using Color = System.Windows.Media.Color;
  11. using Thickness = System.Windows.Thickness;
  12. [assembly: ExportRenderer((typeof(CustomHubTileView)), typeof(CustomHubTileViewRenderer))]
  13. namespace FormulaOneApp.WinPhone.Controls
  14. {
  15. public class CustomHubTileViewRenderer : ViewRenderer<CustomHubTileView, HubTile>
  16. {
  17. private HubTile HubTile;
  18. public CustomHubTileViewRenderer()
  19. {
  20. HubTile = new HubTile
  21. {
  22. Margin = new Thickness(5)
  23. };
  24. }
  25. protected override void OnElementChanged(ElementChangedEventArgs<CustomHubTileView> e)
  26. {
  27. base.OnElementChanged(e);
  28. if (e.OldElement != null || Element == null)
  29. return;
  30. HubTile.Title = Element.Title;
  31. HubTile.Message = Element.Message;
  32. var fileImageSource = Element.Source as FileImageSource;
  33. if (fileImageSource != null)
  34. HubTile.Source = new BitmapImage(new Uri(fileImageSource.File, UriKind.RelativeOrAbsolute));
  35. Color color = Color.FromArgb(
  36. (byte)(Element.Color.A * 255),
  37. (byte)(Element.Color.R * 255),
  38. (byte)(Element.Color.G * 255),
  39. (byte)(Element.Color.B * 255));
  40. HubTile.Background = new SolidColorBrush(color);
  41. SetNativeControl(HubTile);
  42. }
  43. protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  44. {
  45. base.OnElementPropertyChanged(sender, e);
  46. if (Control == null || Element == null)
  47. return;
  48. if (e.PropertyName == CustomHubTileView.TitleProperty.PropertyName)
  49. HubTile.Title = Element.Title;
  50. if (e.PropertyName == CustomHubTileView.MessageProperty.PropertyName)
  51. HubTile.Message = Element.Message;
  52. }
  53. }
  54. }