Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace NewsFeed.Controls
- {
- public class ImageBox : ContentView
- {
- private Image imageElement;
- private Label labelElement;
- private BoxView boxViewElement;
- public static readonly BindableProperty BoxBackgroundColorProperty =
- BindableProperty.Create("BoxBackgroundColor", typeof(Color), typeof(ImageBox), Color.Default);
- public static readonly BindableProperty ImageSourceProperty =
- BindableProperty.Create("ImageSource", typeof(string), typeof(ImageBox), string.Empty);
- public static readonly BindableProperty ContentTextProperty =
- BindableProperty.Create("ContentText", typeof(string), typeof(ImageBox), string.Empty);
- public Color BoxBackgroundColor
- {
- get { return (Color)GetValue(BoxBackgroundColorProperty); }
- set { SetValue(BoxBackgroundColorProperty, value); }
- }
- public string ImageSource
- {
- get { return (string)GetValue(ImageSourceProperty); }
- set { SetValue(ImageSourceProperty, value); }
- }
- public string ContentText
- {
- get { return (string)GetValue(ContentTextProperty); }
- set { SetValue(ContentTextProperty, value); }
- }
- public ImageBox()
- {
- var mainLayout = new RelativeLayout();
- boxViewElement = new BoxView();
- {
- BackgroundColor = BoxBackgroundColor
- };
- mainLayout.Children.Add(boxViewElement,
- Constraint.Constant(0),
- Constraint.Constant(0),
- Constraint.RelativeToParent((p) => p.Width),
- Constraint.RelativeToParent((p) => p.Height));
- imageElement = new Image()
- {
- Aspect = Aspect.AspectFit
- };
- imageElement.Source = ImageSource;
- mainLayout.Children.Add(imageElement,
- Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.2),
- Constraint.RelativeToView(boxViewElement, (p, v) => (v.Height * 0.6) / 2 - v.Width * 0.25),
- Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.6),
- Constraint.RelativeToView(boxViewElement, (p, v) => v.Width * 0.6));
- labelElement = new Label();
- labelElement.FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label));
- labelElement.Text = ContentText;
- labelElement.TextColor = Color.White;
- labelElement.HorizontalTextAlignment = TextAlignment.Center;
- labelElement.VerticalTextAlignment = TextAlignment.Center;
- mainLayout.Children.Add(labelElement,
- Constraint.RelativeToParent((p) => p.Width * 0.025),
- Constraint.RelativeToParent((p) => p.Height * 0.625),
- Constraint.RelativeToParent((p) => p.Width * 0.95),
- Constraint.RelativeToParent((p) => p.Height * 0.325));
- Content = mainLayout;
- }
- protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- base.OnPropertyChanged(propertyName);
- if (propertyName.Equals(BoxBackgroundColorProperty.PropertyName))
- {
- boxViewElement.BackgroundColor = BoxBackgroundColor;
- }
- else if (propertyName.Equals(ImageSourceProperty.PropertyName))
- {
- imageElement.Source = ImageSource;
- }
- else if (propertyName.Equals(ContentTextProperty.PropertyName))
- {
- labelElement.Text = ContentText;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment