问题描述
在 Xamarin.Forms 中,以下属性获得双精度值:WidthRequest、HeightRequest、Padding、Spacing 等.这个数字是什么意思?是像素还是其他?我放在这些属性中的值是否响应设备屏幕尺寸?考虑到可用的多种屏幕尺寸,我应该如何决定应该使用什么值?
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?
我也尝试打印一些元素的宽度,结果得到 -1.为什么?
I also tried to print some elements' width, and got -1 as a result. Why?
推荐答案
Xamarin.Forms 的理念是尽可能多地使用底层平台的约定可能的.根据这一理念,Xamarin.Forms 程序员使用定义的大小通过每个特定平台.程序员通过 Xamarin.Forms API 遇到的所有尺寸位于这些平台特定的设备独立单元中.(c)
Xamarin.Forms has a philosophy of using the conventions of the underlying platforms as much aspossible. In accordance with this philosophy, the Xamarin.Forms programmer works with sizes definedby each particular platform. All sizes that the programmer encounters through the Xamarin.Forms APIare in these platform-specific device-independent units.(c)
在 Xamarin Forms 中,这些数字与特定平台上的英寸和厘米有关.见下文:
In Xamarin Forms those numbers have relationships to inches and centimeters on a specific platform. See below:
这些是以英寸
- iOS:160 单位到英寸
- Android:每英寸 160 个单位
- Windows Phone:每英寸 240 个单位
如果您喜欢公制,这些是以厘米为单位的关系
These are relations in centimeters if you prefer metric system
- iOS:64 个单位为厘米
- Android:64 个单位为厘米
- Windows Phone:96 单位为厘米
例如,如果您希望显示宽度为 1 英寸、高度为 2 英寸的图像,您可以这样做:
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)
};
同样的概念适用于间距和填充.这些参数也有默认值.初始设置是 –1 的模拟"值.这些属性的值仅在布局系统定位和调整页面上的所有内容时才有效.
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.
希望这有帮助!
您可以从名为Creating Mobile Apps with Xamarin Forms"一书中了解更多相关信息
You can learn more about it from book called "Creating Mobile Apps with Xamarin Forms"
这篇关于Xamarin.Forms WidthRequest 值含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!