I know the units in each platfform and I only want to know how to get units from pixels
You could use Xamarin.Essentials to get the Screen density
as follows:
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Screen density
var density = mainDisplayInfo.Density;
If the density is 3
in iOS device and pixels is 88
, then there are 88/3
units in iOS.
If the phone is 360dpi
That means it shoule be a Android device, and screen density also can be calculated by 360/160 = 2.25
. Then there are 88/2.25 units to set for HeightRequest
.
================================Update==============================
If there is a BoxView
in Xaml as follows:
<BoxView x:Name="MyBoxView" BackgroundColor="CadetBlue" HorizontalOptions="Fill"/>
And the effect:
Now I will print the MyBoxView.Width
. the result is:
Console.WriteLine("++++MyBoxView++++" + MyBoxView.Width);
++++MyBoxView++++411.428571428571
If you use var density = mainDisplayInfo.Density;
to get density, you will get Screen Density::2.625
. (My device is Piexl_2_pie_9_0 api 28 emulator)
You know the size of screen width is 1080
pixels, however the width is 411.428571428571
. That should means units of WidthRequest
.
And if you put 411.428571428571 * 2.625
, you will get 1080
pixels.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…