How to get/detect screen size in Xamarin.Forms?

前端 未结 6 819
[愿得一人]
[愿得一人] 2020-12-08 22:24

I am trying to rewrite an app that I wrote for iOS. I was going to write an android version but thought It\'d be better to make this the opportunity to use Xamarin.Forms. Do

6条回答
  •  情深已故
    2020-12-08 22:35

    Updating for future developers trying to do this that either haven't looked or missed it. The Page class inherits from VisualElement, which now has two properties (which happen to be bindable for use in XAML):

    Width - Gets the current rendered width of this element. This is a read-only bindable property. Height - Gets the current rendered height of this element. This is a read-only bindable property.

    in your page codebehind you would just do something like:

    var myWidth = this.Width;
    var myHeight = this.Height;
    

    if you needed to know if it changes, use the SizeChanged event:

    public MyPage()
    {
        SizeChanged += OnSizeChanged;
    }
    
    private void OnSizeChanged(object sender, EventArgs e)
    {
        var myWidth = this.Width;
        var myHeight = this.Height;
    }
    

    ref: Microsoft Docs

提交回复
热议问题