Joost Schepel
Joost Schepel

Reputation: 646

Xamarin Forms vertical align image at bottom programmatically

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

Upvotes: 0

Views: 5913

Answers (1)

Simon
Simon

Reputation: 66

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

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.

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.RelativeToParent and Constraint.RelativeToView are useful in simple cases and Expression trees will solve arbitrary layout problems.

Upvotes: 5

Related Questions