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

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

    Recipe

    Create a new Xamarin.Android application named ScreenSize. Edit Main.axml so that it contains two TextViews:

    
    
        
        
    
    
    1. Edit Activity1.cs, change the code in OnCreate to the following:

      ![enter image description here][1] private int ConvertPixelsToDp(float pixelValue) { var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density); return dp; }

    2. Run the application. Depending on the device, it will display the screen height and width. The following screen shot is from a Galaxy Nexus:

    [image link] http://i.stack.imgur.com/TQhba.png

提交回复
热议问题