在Xamarin中,有没有办法在使用RelativeLayout的过程中使用xaml中的MaxY 属性?

In Xamarin, is there any way to use the MaxY Property in xaml during the use of a RelativeLayout?

我想这样做:

         RelativeLayout.YConstraint="{ ConstraintExpression
             Type=RelativeToView,
             ElementName=info_box,
             Property=Y+Height,
             Factor=1,
             Constant=22
         }

有什么方法可以在 xaml 中实现这一点,还是我太依赖 xaml 并且不应该担心它并在代码中做这样的事情?

恐怕你必须在代码后面做这件事,这是我试过的一个例子:

public MainPage()
{
    InitializeComponent();

    RelativeLayout layout = new RelativeLayout();

    BoxView redBox = new BoxView() {BackgroundColor = Color.Red };
    BoxView blueBox = new BoxView() { BackgroundColor = Color.Blue };

    layout.Children.Add(redBox, Constraint.RelativeToParent((parent) => {
        return parent.X;
    }), Constraint.RelativeToParent((parent) => {
        return parent.Y * .15;
    }), Constraint.RelativeToParent((parent) => {
        return parent.Width;
    }), Constraint.RelativeToParent((parent) => {
        return parent.Height * .8;
    }));

    layout.Children.Add(blueBox, Constraint.RelativeToView(redBox, (Parent, sibling) => {
        return sibling.X + 20;
    }), Constraint.RelativeToView(redBox, (parent, sibling) => {
        return sibling.Y + redBox.Height;
    }), Constraint.RelativeToParent((parent) => {
        return parent.Width * .5;
    }), Constraint.RelativeToParent((parent) => {
        return parent.Height * .5;
    }));

    Content = layout;
}

在xaml中定义控件,并在后面的代码中更改YConstraint

    RelativeLayout.SetYConstraint(blueBox, Constraint.RelativeToView(redBox, (parent, sibling) =>
    {
        return sibling.Y + sibling.Height;
    }));

在xaml中:

<RelativeLayout>
    <BoxView Color="Red" x:Name="redBox"
    RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
        Property=Height,Factor=.15,Constant=0}"
    RelativeLayout.WidthConstraint="{ConstraintExpression
        Type=RelativeToParent,Property=Width,Factor=1,Constant=0}"
    RelativeLayout.HeightConstraint="{ConstraintExpression
        Type=RelativeToParent,Property=Height,Factor=.4,Constant=0}" />

    <BoxView Color="Blue" x:Name="blueBox"

    RelativeLayout.YConstraint="{ ConstraintExpression Type=RelativeToView,ElementName=redBox,Property=Y,Factor= 1, Constant= 0}"                
    RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView,
        ElementName=redBox,Property=X,Factor=1,Constant=20}"
    RelativeLayout.WidthConstraint="{ConstraintExpression
        Type=RelativeToParent,Property=Width,Factor=.5,Constant=0}"
    RelativeLayout.HeightConstraint="{ConstraintExpression
        Type=RelativeToParent,Property=Height,Factor=.5,Constant=0}" />
</RelativeLayout>

顺便说一句,您可以使用 Grid(单列)或 Vertical StackLayout 轻松实现此布局。