Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

c# - Xamarin.Forms WidthRequest value meaning

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...