我有一个在XAML文件中创建的对象(在相对布局中)...
<Button
x:Name="btnForms"
BackgroundColor="Blue"
RelativeLayout.XConstraint = "{ConstraintExpression Type=RelativeToParent, Factor=.4, Property=Width}"
RelativeLayout.YConstraint = "{ConstraintExpression Type=RelativeToParent, Factor=.4, Property=Height}"
RelativeLayout.WidthConstraint = "{ConstraintExpression Type=RelativeToParent, Factor=.2, Property=Height}"
RelativeLayout.HeightConstraint = "{ConstraintExpression Type=RelativeToParent, Factor=.2, Property=Height}"
/>
...当我在UWP上运行并更改窗口大小时,按钮会在我更改窗口大小后立即移动并调整大小 . 像我期望的那样工作 .
但是,如果我在XAML中这样做...
<Button
x:Name="btnForms"
BackgroundColor="Blue"
/>
然后在后面的代码中调整它的大小,像这样......
private void TestLayout_LayoutChanged(object sender, EventArgs e)
{
...
...
...
var constraints = new Rectangle(this.Width * .4,
this.Height * .4,
this.Height * .2,
this.Height * .2);
ChangeConstraints(btnForms, constraints);
...
...
...
}
private void ChangeConstraints(BindableObject obj, Rectangle rect)
{
var cons = BoundsConstraint.FromExpression(() => rect, new View[0]);
RelativeLayout.SetBoundsConstraint(obj, cons);
}
当我调整窗口大小时,按钮不会调整大小或重新定位 . 直到我第二次调整窗口大小 . 每次调整窗口大小时,我都必须按两次按钮来更新其大小和位置 .
我错过了SetBoundsConstraint的东西吗?有没有不同的方法来更新代码背后的相对布局?有谁知道为什么会出现这个问题?