Xamarin.Forms WidthRequest value meaning

10,041

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"

Share:
10,041

Related videos on Youtube

Or Harel
Author by

Or Harel

Been there, done that. Now developing web apps.

Updated on September 14, 2022

Comments

  • Or Harel
    Or Harel over 1 year

    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?