Xamarin Forms - Get the size of an AbsoluteLayout

Emixam23Emixam23 USMember ✭✭✭

Hi,

I cannot find the way to get the Width and the Height of a Xamarin.Forms.View.

I have this object:

public class SquareLayout : View
{
    public static readonly BindableProperty ScalingBaseProperty =
        BindableProperty.Create(nameof(ScalingBase), typeof(ScalingBaseType), typeof(SquareLayout), ScalingBaseType.Average,
            propertyChanged: OnScalingBasePropertyChanged);

    public ScalingBaseType ScalingBase
    {
        get { return (ScalingBaseType)GetValue(ScalingBaseProperty); }
        set { SetValue(ScalingBaseProperty, value); }
    }

    public enum ScalingBaseType
    {
        Average,
        Height,
        Width
    }

    public static void OnScalingBasePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        SquareLayout sq = ((SquareLayout)bindable);

        Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height);

        switch (sq.ScalingBase)
        {
            case ScalingBaseType.Average:
                double size = (sq.Bounds.Width + sq.Bounds.Height) / 2;
                sq.WidthRequest = size;
                sq.HeightRequest = size;
                break;
            case ScalingBaseType.Height:
                sq.WidthRequest = sq.Bounds.Height;
                break;
            case ScalingBaseType.Width:
                sq.HeightRequest = sq.Bounds.Width;
                break;
        }

        Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height);
    }
}

Basically, you declare somewhere this SquareLayout and then, based on an Enum, the layout is resized as a Square.

So, I then have this XAML part

<ContentPage.Content>
  <AbsoluteLayout BackgroundColor="White">
  ...
    <AbsoluteLayout x:Name="ToolsLayout" BackgroundColor="Red"
                    AbsoluteLayout.LayoutBounds="0.5, 0.05, 0.9, 0.075"
                    AbsoluteLayout.LayoutFlags="All">
      <control:SquareLayout BackgroundColor="White" ScalingBase="Height"
                            AbsoluteLayout.LayoutBounds="0, 0.5, 0.1, 1"
                            AbsoluteLayout.LayoutFlags="All">

      </control:SquareLayout>
      <control:SquareLayout BackgroundColor="White" ScalingBase="Height"
                            AbsoluteLayout.LayoutBounds="1, 0.5, 0.1, 1"
                            AbsoluteLayout.LayoutFlags="All">

      </control:SquareLayout>
    </AbsoluteLayout>
  ...
  </AbsoluteLayout>
</ContentPage.Content>

Which would give me a Rectangle with two Square around.. But nothing !

I tried to display the Width and the Height but nothing.. I tried the Bounds as well as you can see in the example, but nothing one more time..

The only values I get are -1

Does someone can help me? Thank in advance !

Tagged:

Answers

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    Try this in your SquareLayout:

            protected override void OnSizeAllocated (double width, double height)
            {
                base.OnSizeAllocated (width, height);
                //this.Debug (this.GetType ().Name + " A {" + width + "x" + height + "}");
            }
    
  • Emixam23Emixam23 USMember ✭✭✭

    Thank ! I got the right values now, but the size doesn't seems to change when I set HeightRequest and WidthRequest, why? :/

Sign In or Register to comment.