0

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

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

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

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

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

1 回答 1

5

在 Xamarin Studio 中的程序集浏览器中,您的 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);

其中 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)
    }
)

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

于 2014-06-06T03:36:59.413 回答