Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.ScreenWidth with Xamarin Forms

Referencing code found at...Highlight a Route on a Map

They show...

var customMap = new CustomMap
{
    WidthRequest = App.ScreenWidth
};

App.ScreenWidth isn't available any longer. Has it been replaced with Application.Current.MainPage.Width?

like image 519
John Livermore Avatar asked Dec 05 '22 16:12

John Livermore


2 Answers

In that demo, App.ScreenWidth and App.ScreenHeight are static variables defined in the App class and assigned in the native projects:

iOS app project:

App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = UIScreen.MainScreen.Bounds.Height

Android app project:

App.ScreenWidth = (width - 0.5f) / density;
App.ScreenHeight = (height - 0.5f) / density;

Ref: https://github.com/xamarin/recipes/search?p=2&q=ScreenWidth&utf8=✓

like image 162
SushiHangover Avatar answered Dec 27 '22 02:12

SushiHangover


Most simplest and accurate way to get device height & width in PCL:

using Xamarin.Forms;

namespace ABC
{
    public class MyPage : ContentPage
    {
        private double _width;
        private double _height;

        public MyPage()
        {
            Content = new Label 
            {
                WidthRequest = _width,
                Text = "Welcome to Xamarin.Forms!"
            };
        }

        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            _width = width;
            _height = height;
        }
    }
}
like image 28
Jay Patel Avatar answered Dec 27 '22 00:12

Jay Patel