In Xamarin.Forms, the following properties get a double: WidthRequest, HeightRequest, Padding, Spacing, etc. What's the meaning of that number? Is it pixels, or something else? Is the value that I put in those properties responsive to the device screen size? How should I decide what value I should use, taking into consideration the many screen sizes available?
I also tried to print some elements' width, and got -1 as a result. Why?
Xamarin.Forms has a philosophy of using the conventions of the underlying platforms as much as possible. In accordance with this philosophy, the Xamarin.Forms programmer works with sizes defined by each particular platform. All sizes that the programmer encounters through the Xamarin.Forms API are in these platform-specific device-independent units.(c)
In Xamarin Forms those numbers have relationships to inches and centimeters on a specific platform. See below:
These are relations in inches
These are relations in centimeters if you prefer metric system
For Example if you want your to display an Image with width of 1 inch and height of 2 inches you would do:
var avatar = new Image{ WidthRequest = Device.OnPlatform(160, 160, 240), HeightRequest = Device.OnPlatform(320, 320, 480) };
Same concept applies to Spacing and Padding. These Parameters also have default values. The initial settings are “mock” values of –1. The values of these properties only become valid when the layout system has positioned and sized everything on the page.
Hope this helps!
You can learn more about it from book called "Creating Mobile Apps with Xamarin Forms"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With