Hello,
Welcome to our Microsoft Q&A platform!
The view is rendered after it's initialized. So the values of the parameters will be null(or default values) if you set data to the views in the constructor method of the custom control. To avoid this, try using data binding instead. Set the custom control as the BindingContext
, the binding paths are the name of the parameters. The layout's direction is defined accroding to the value of the alignment, you could detect the property changed event to check the value.
Here is the sample code, you could refer to it.
public class ImageLabel : CustomUI
{
//change the _layout to be a static property
public static FlexLayout _layout;
//add property changed event to ImageAlignmentProperty to set value to '_layout.Direction' according to the alignment value
public static readonly BindableProperty ImageAlignmentProperty = BindableProperty.Create(nameof(ImageAlignment), typeof(Alignment), typeof(ImageLabel), propertyChanged: OnImageAlignmentChanged);
//property changed event
private static void OnImageAlignmentChanged(BindableObject bindable, object oldValue, object newValue)
{
var alignment = (Alignment)newValue;
_layout.Direction = (alignment == Alignment.Up || alignment == Alignment.Down) ? FlexDirection.Column : FlexDirection.Row;
}
public ImageLabel()
{
_layout = new FlexLayout
{
//Direction = this._getFlexDir(ImageAlignment),
JustifyContent = FlexJustify.Center,
AlignItems = FlexAlignItems.Center,
AlignContent = FlexAlignContent.Center,
Padding = this.Padding,
BackgroundColor = Color.Blue
};
_layout.SetBinding(FlexLayout.PaddingProperty, "Padding");
var _frame = new Frame
{
//CornerRadius = ImageCornerRadius,
//Padding = ImagePadding,
//BackgroundColor = ImageBackgroundColor
};
_frame.SetBinding(Frame.CornerRadiusProperty, "ImageCornerRadius");
_frame.SetBinding(Frame.PaddingProperty, "ImagePadding");
_frame.SetBinding(Frame.BackgroundColorProperty, "ImageBackgroundColor");
Image img = new Image
{
//Margin = Margin,
//HeightRequest = ImgHeight,
//Source = Source
};
img.SetBinding(Image.MarginProperty, "Margin");
img.SetBinding(Image.HeightProperty, "ImgHeight");
img.SetBinding(Image.SourceProperty, "Source");
_frame.Content = img;
var _label = new Label
{
//FontFamily = this.FontFamily,
//FontSize = this.FontSize,
//Text = this.Text,
//TextColor = this.TextColor,
//Padding = this.TextPadding,
//FontAttributes = this.TextAttributes,
};
_label.SetBinding(Label.TextProperty, "Text");
_label.SetBinding(Label.FontFamilyProperty, "FontFamily");
_label.SetBinding(Label.TextColorProperty, "TextColor");
_label.SetBinding(Label.PaddingProperty, "TextPadding");
_label.SetBinding(Label.FontAttributesProperty, "TextAttributes");
if (ImageAlignment == Alignment.Up || ImageAlignment == Alignment.Left)
{
_layout.Children.Add(_frame);
_layout.Children.Add(_label);
}
else
{
_layout.Children.Add(_label);
_layout.Children.Add(_frame);
}
this.Content = _layout;
BindingContext = this;
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.