Xamarin以编程方式在底部形成垂直对齐图像

Joo*_*pel 0 xamarin xamarin.forms

在(相对)新的Xamarin表单中,我试图将图像垂直对齐到滚动视图的底部.

这是我的代码,这正是我想要的(对于滚动的大图像).但是当我的图像小于设备的高度时,它应该对准屏幕的底部,而不是屏幕的中心(可能是默认值).由于文档(仍然)缺乏,我如何在代码中实现这一点?

return new ContentPage {
    Content = new ScrollView {
        Orientation = ScrollOrientation.Vertical,
        BackgroundColor = Color.Black,
        Content = new Image {
            Source = ImageSource.FromFile (image)
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

我已经尝试过这个,但它给出了一个错误,即以下方法或属性之间的调用是不明确的...

RelativeLayout rl = new RelativeLayout ();
rl.Children.Add (new ScrollView {
    Orientation = ScrollOrientation.Vertical,
    BackgroundColor = Color.Black,
    Content = new Image {
        Source = ImageSource.FromFile (image)
    }
});

return new ContentPage {
    Content = rl
};
Run Code Online (Sandbox Code Playgroud)

小智 5

从Xamarin Studio的Assembly Browser中,您对Children.Add的选项是:

void Add(T view, Expression<Func<Rectangle>> bounds);
void Add(T view, Expression<Func<double>> x = null, Expression<Func<double>> y = null, Expression<Func<double>> width = null, Expression<Func<double>> height = null);
void Add(T view, Constraint xConstraint = null, Constraint yConstraint = null, Constraint widthConstraint = null, Constraint heightConstraint = null);
Run Code Online (Sandbox Code Playgroud)

Expression是System.Linq.Expresssions中的类型.

您收到了一个模糊的调用错误,因为除了视图之外,所有这些重载都具有默认值.

为了使用Children的对象初始值设定项,您需要传入表达式或约束,如:

rl.Children.Add (
    {
        new ScrollView {
            Orientation = ScrollOrientation.Vertical,
            BackgroundColor = Color.Black,
            Content = new Image {
                Source = ImageSource.FromFile (image)
            }
        },
        Constraint.Constant(0),
        Constraint.Constant(0)
    }
)
Run Code Online (Sandbox Code Playgroud)

Constraint.RelativeToParent并且Constraint.RelativeToView在简单的情况下很有用,表达式树将解决任意布局问题.