Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to get Device Width/Height?

$
0
0

I'm trying to get the Device Width/Height, but everything I do comes back with -1

I'm trying

public class SomePage: ContentPage
{
    private readonly StackLayout content;
    public SomePage()
    {
        content = new StackLayout
        {
            HorizontalOptions = LayoutOptions.StartAndExpand, VerticalOptions = LayoutOptions.StartAndExpand
        };
        var width1 = Width; //-1
        var width2 = content.Width; //-1
        var width3 = Bounds.Width; //-1
        var width4 = content.Bounds.Width; //-1
    }
    protected override void OnAppearing()
    {
        var width5 = Width; //-1
        var width6 = Bounds.Width; //-1
        var width7 = content.Width; //-1
        var width8 = content.Bounds.Width; //-1
        var width9 = Content.Width; //-1
        var width10 = Content.Bounds.Width; //-1
    }
}

Non of this seems to work.

Ideally there should be something in the Xamarin.Forms.Device Namespace... Something like

public static readonly Xamarin.Forms.Rectangle Bounds {get { return // device specific bounds; }}

With the simple ability to call it.

var deviceWidth = Xamarin.Forms.Device.Bounds.Width;

Viewing all articles
Browse latest Browse all 91519

Trending Articles