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

前端 未结 6 815
[愿得一人]
[愿得一人] 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:34

    In your App file define a public static variable

    public static Size ScreenSize;
    

    You should initialise the variable both in IOS and Android before calling the App constructer. For IOS, in FinishedLaunching method

    App.ScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
    

    and For Android, in OnCreate function

                App.ScreenSize = new Xamarin.Forms.Size((int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density), (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density));
    

提交回复
热议问题