Xamarin以编程方式在底部形成垂直对齐图像 [英] Xamarin Forms vertical align image at bottom programmatically

查看:61
本文介绍了Xamarin以编程方式在底部形成垂直对齐图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the (relatively) new Xamarin Forms I'm trying to vertically align an image to the bottom of a scrollview.

这是我的代码,它完全符合我的要求(对于较大的图像,它会滚动).但是,当我的图像小于设备的高度时,它应与屏幕的底部对齐,而不是与屏幕的中心(可能是默认值)对齐.由于(仍然)缺少文档,如何在代码中实现?

This is my code, this does exactly what I want (for larger images it scrolls). But when I have an image which is smaller than the height of the device, it should align to the bottom of the screen, instead of the center (probably default) of the screen. Since the documentation is (still) lacking, how can I achieve this in code?

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

我对此进行了尝试,但是它给出了以下方法或属性之间的调用不明确的错误消息……

I've tried it with this, but it gives an error that the call is ambiguous between the following methods or properties...

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
};

推荐答案

从Xamarin Studio的Assembly Browser中,选择Children.Add的选项为:

From the Assembly Browser in Xamarin Studio, your options for Children.Add are:

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);

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

Where Expression is the type from System.Linq.Expresssions.

您收到了一个模棱两可的调用错误,因为所有这些重载都具有除视图以外的所有参数的默认值.

You received an ambiguous call error because all these overloads have default values for all parameters besides view.

为了将对象初始化程序用于Children,您需要传递表达式或约束,例如:

In order to use the object initializer for Children, you'll need to pass in your expressions or constraints like:

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

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

Constraint.RelativeToParent and Constraint.RelativeToView are useful in simple cases and Expression trees will solve arbitrary layout problems.

这篇关于Xamarin以编程方式在底部形成垂直对齐图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆