Reputation: 458
I'm trying to get the width of my imageviews at android platform. The width is "-1". I tried this in the OnAppearing() method. It's working fine at iOS platform.
c#
protected override void OnAppearing()
{
base.OnAppearing();
myWidth = imagePusher.Width; //Width = -1 at android
}
XAML
...
<ScrollView x:Name="scrollPusher" Orientation="Horizontal" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}">
<RelativeLayout x:Name="rlPusher">
<StackLayout x:Name="stackPusher" Orientation="Horizontal" Spacing="0">
<Image x:Name="imagePusherSpacer" Source="pusherSpacer.jpg" Aspect="AspectFit">
</Image>
<Image x:Name="imagePusher" Source="pusher.jpg" Aspect="AspectFit">
</Image>
</StackLayout>
</RelativeLayout>
</ScrollView>
...
Thanks for the help.
Upvotes: 1
Views: 1135
Reputation: 9703
You can either subscribe to the SizeChanged
event (see here)
private void Image_OnSizeChanged(object sender, EventArgs e)
{
// image size should be set here
}
or override OnSizeAllocated
(see here)
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (this.width != width || this.height != height)
{
this.width = width;
this.height = height;
// do what ever you like here
}
}
Size should eventually be correct with either of them. Please note with the second method, you won't get the actual size with the first call. You may have to check for width/height being equal to -1.
Upvotes: 2