Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms WidthRequest value meaning

Tags:

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?

like image 200
Or Harel Avatar asked Apr 22 '15 20:04

Or Harel


1 Answers

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

  • iOS: 160 units to the inch
  • Android: 160 units to the inch
  • Windows Phone: 240 units to the inch

These are relations in centimeters if you prefer metric system

  • iOS: 64 units to the centimeter
  • Android: 64 units to the centimeters
  • Windows Phone: 96 units to the centimeters

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"

like image 116
Imad Alazani Avatar answered Sep 19 '22 15:09

Imad Alazani