Guest User

ImageBox

a guest
May 3rd, 2016
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8.  
  9. namespace NewsFeed.Controls
  10. {
  11.     public class ImageBox : ContentView
  12.     {
  13.         private Image imageElement;
  14.         private Label labelElement;
  15.         private BoxView boxViewElement;
  16.  
  17.         public static readonly BindableProperty BoxBackgroundColorProperty =
  18.             BindableProperty.Create("BoxBackgroundColor", typeof(Color), typeof(ImageBox), Color.Default);
  19.         public static readonly BindableProperty ImageSourceProperty =
  20.             BindableProperty.Create("ImageSource", typeof(string), typeof(ImageBox), string.Empty);
  21.         public static readonly BindableProperty ContentTextProperty =
  22.             BindableProperty.Create("ContentText", typeof(string), typeof(ImageBox), string.Empty);
  23.  
  24.         public Color BoxBackgroundColor
  25.         {
  26.             get { return (Color)GetValue(BoxBackgroundColorProperty); }
  27.             set { SetValue(BoxBackgroundColorProperty, value); }
  28.         }
  29.  
  30.         public string ImageSource
  31.         {
  32.             get { return (string)GetValue(ImageSourceProperty); }
  33.             set { SetValue(ImageSourceProperty, value); }
  34.         }
  35.         public string ContentText
  36.         {
  37.             get { return (string)GetValue(ContentTextProperty); }
  38.             set { SetValue(ContentTextProperty, value); }
  39.         }
  40.  
  41.         public ImageBox()
  42.         {
  43.             var mainLayout = new RelativeLayout();
  44.  
  45.             boxViewElement = new BoxView();
  46.             {
  47.                 BackgroundColor = BoxBackgroundColor
  48.             };
  49.             mainLayout.Children.Add(boxViewElement,
  50.                 Constraint.Constant(0),
  51.                 Constraint.Constant(0),
  52.                 Constraint.RelativeToParent((p) => p.Width),
  53.                 Constraint.RelativeToParent((p) => p.Height));
  54.  
  55.             imageElement = new Image()
  56.             {
  57.                 Aspect = Aspect.AspectFit
  58.             };
  59.             imageElement.Source = ImageSource;
  60.             mainLayout.Children.Add(imageElement,
  61.                 Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.2),
  62.                 Constraint.RelativeToView(boxViewElement, (p, v) => (v.Height * 0.6) / 2 - v.Width * 0.25),
  63.                 Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.6),
  64.                 Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.6));
  65.  
  66.             labelElement = new Label();
  67.             labelElement.FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label));
  68.             labelElement.Text = ContentText;
  69.             labelElement.TextColor = Color.White;
  70.             labelElement.HorizontalTextAlignment = TextAlignment.Center;
  71.             labelElement.VerticalTextAlignment = TextAlignment.Center;
  72.  
  73.             mainLayout.Children.Add(labelElement,
  74.                 Constraint.RelativeToParent((p) => p.Width * 0.025),
  75.                 Constraint.RelativeToParent((p) => p.Height * 0.625),
  76.                 Constraint.RelativeToParent((p) => p.Width * 0.95),
  77.                 Constraint.RelativeToParent((p) => p.Height * 0.325));
  78.  
  79.             Content = mainLayout;
  80.         }
  81.  
  82.         protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
  83.         {
  84.             base.OnPropertyChanged(propertyName);
  85.             if (propertyName.Equals(BoxBackgroundColorProperty.PropertyName))
  86.             {
  87.                 boxViewElement.BackgroundColor = BoxBackgroundColor;
  88.             }
  89.             else if (propertyName.Equals(ImageSourceProperty.PropertyName))
  90.             {
  91.                 imageElement.Source = ImageSource;
  92.             }
  93.             else if (propertyName.Equals(ContentTextProperty.PropertyName))
  94.             {
  95.                 labelElement.Text = ContentText;
  96.             }
  97.         }
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment