gpt4 book ai didi

c# - Xamarin.Forms:如何使用相对布局使 View 居中? `Width` 和 `Height` 返回 -1

转载 作者:太空狗 更新时间:2023-10-29 19:58:03 26 4
gpt4 key购买 nike

当尝试在 Xamarin.Forms 中使用控件的 HeightWidth 属性时,两者都返回 -1,这会导致相对布局出现偏心在屏幕上。

var mainLayout = new RelativeLayout();

//Add the Switch to the center of the screen
mainLayout.Children.Add(mySwitch,
Constraint.RelativeToParent(parent => parent.Width / 2 - mySwitch.Width / 2),
Constraint.RelativeToParent(parent => parent.Height / 2 - mySwitch.Height / 2));

//Add a Label below the switch
mainLayout.Children.Add(switchLabel,
Constraint.RelativeToParent(parent => parent.Width / 2 - switchLabel.Width / 2),
Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + mySwitch.Height + 10));

Content = mainLayout;

enter image description here

最佳答案

为什么 Xamarin.Forms 为 Height 返回 -1和 Width

Xamarin.Forms 返回 -1 作为这些属性的默认值,并且在 Xamarin.Forms 创建 native 控件之前一直保持 -1,例如UIButton,并将该原生控件添加到布局中。

在此链接中,您可以看到 Xamarin.Forms 源代码返回 -1作为默认值: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/VisualElement.cs

使用相对布局约束 View 的最佳方式

选项 1:局部函数(需要 C# 7.0 或更高版本)

使用 Local Function动态检索 WidthHeight属性

var mainLayout = new RelativeLayout();

//Add the Switch to the center of the screen
mainLayout.Children.Add(mySwitch,
Constraint.RelativeToParent(parent => parent.Width / 2 - getWidth(parent, mySwitch)/ 2),
Constraint.RelativeToParent(parent => parent.Height / 2 - getHeight(parent, mySwitch) / 2));

//Add a Label below the switch
mainLayout.Children.Add(switchLabel,
Constraint.RelativeToParent(parent => parent.Width / 2 - getWidth(parent, switchLabel) / 2),
Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + getHeight(parent, mySwitch) + 10));

Content = mainLayout;

static double getWidth(RelativeLayout parent, View view) => view?.Measure(parent.Width, parent.Height).Request.Width ?? -1;
static double getHeight(RelativeLayout parent, View view) => view?.Measure(parent.Width, parent.Height).Request.Height ?? -1;

选项 2:Func<RelativeLayout, double>

使用 Func动态检索 WidthHeight属性

var mainLayout = new RelativeLayout();

Func<RelativeLayout, double> getSwitchWidth = (parent) => mySwitch.Measure(parent.Width, parent.Height).Request.Width;
Func<RelativeLayout, double> getSwitchHeight = (parent) => mySwitch.Measure(parent.Width, parent.Height).Request.Height;
Func<RelativeLayout, double> getLabelWidth = (parent) => switchLabel.Measure(parent.Width, parent.Height).Request.Width;
Func<RelativeLayout, double> getLabelHeight = (parent) => switchLabel.Measure(parent.Width, parent.Height).Request.Height;

//Add the Switch to the center of the screen
mainLayout.Children.Add(mySwitch,
Constraint.RelativeToParent(parent => parent.Width / 2 - getSwitchWidth(parent)/ 2),
Constraint.RelativeToParent(parent => parent.Height / 2 - getSwitchHeight(parent) / 2));

//Add a Label below the switch
mainLayout.Children.Add(switchLabel,
Constraint.RelativeToParent(parent => parent.Width / 2 - getLabelWidth(parent) / 2),
Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + getSwitchHeight(parent) + 10));

Content = mainLayout;

enter image description here

感谢@BrewMate感谢教我这个技巧!

关于c# - Xamarin.Forms:如何使用相对布局使 View 居中? `Width` 和 `Height` 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com