XAML 中可以使用百分比值吗?
在 html 中可以说 width="20%"。这在 XAML 中当然是不允许的。有谁知道为什么会这样,或者有没有办法在 XAML 中获得百分比值支持?
In html one can say width="20%". This is not allowed in XAML of course. Does anyone know why that is or is there a way to get percentage value support in XAML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
网格列定义和行定义允许比例单位(除了固定像素和自动)。
以下是 2 个示例:
第一列将根据需要设置大小,以容纳该列中的所有内容。下一列的宽度为 20 个与设备无关的像素。网格的剩余宽度将在剩余的列之间平均分配。 (每列 100% / 4 = 25%)
此代码会将 4 列分为总网格宽度的 10%、40%、40% 和 10%。
Grid ColumnDefinitions and RowDefinitions allow for proportional units (in addition to fixed pixels and Auto).
Here are 2 examples:
The first column will be as large as necessary to fit all content in the column. The next column is 20 device-independant pixels wide. The remaining Width of the grid will be divided equally among the remaining columns. (100% / 4 = 25% in each)
This code would divide 4 columns into 10%, 40%, 40%, and 10% of the total Grid Width.
如果您不使用网格,那么您可以使用 Xamarin.Essentials 获取以像素为单位的实际宽度/高度,然后除以密度并乘以所需的尺寸,
即
以像素为单位的实际屏幕宽度/密度 * 所需的百分比
我的示例是 90% 绑定到 VM 中的此计算
屏幕宽度,您可以从 Xaml Xaml
VM
我刚刚开始使用 XF 4.8 在 iOS 和 Android 上运行的应用程序中使用此计算,并且它可以正常工作,将弹出页面的大小设置为屏幕宽度的 90%可用屏幕宽度
在平板电脑和手机上进行了测试
If you aren't using Grids then you can use Xamarin.Essentials to get the real width/height in pixels then divide by the density and multiply by your desired dimensions
i.e.
Real screen width in pixels / density * percentage required
My example is 90% of screen width, you could bind to this calculation in a VM from Xaml
Xaml
VM
I've just started using this for our app running on iOS and Android using XF 4.8 and it is working correctly to size a popup page to 90% of the available screenwidth
Tested on tablets and phones
您最好采用编程方式来解决该问题。
为元素分配半个屏幕大小的示例:
限制是它无法通过直接绑定到 XAML 来应用,但可以轻松解决相关问题,并且可以分配给任何视图尺寸(当然)。永远要明白,事物的复杂应用是基于像这样的简单计算,并且因此可以很容易地应用。请随意将其应用于根据任何百分比计算重载高度或宽度的继承类。
You are better off doing a programmatic way of going about the solution.
Example to assign a half screen size to element:
The restriction is that this doesn't have an ability to apply through binding to XAML directly, but easily resolves the problem associated and can be assigned to any view dimensions (of course). Always understand that complicated applications of things are based on simpler calculations like this and can be readily applied because of it. Feel free to apply this to an inheriting class that overloads the height or width according to any percentage calcs.