Issue
In my Xamarin Forms 5 app, I set a background image in code behind using the following code:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyPage : ContentPage
{
public MyPage()
{
InitializeComponent();
this.BackgroundImageSource = "my_bg_image.jpg";
}
}
How do I set the image aspect ratio in code behind? I need to set it to AspectFill.
Solution
As @ToolmakerSteve mentioned, there's no way to set Aspect for BackgroundImageSource. The following code works fine, both in terms of setting the background image correctly and handling the keyboard covering the form elements issue on iOS.
<RelativeLayout>
<Image
Source="my_background_image.jpg"
Aspect="AspectFill"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
<ScrollView
Orientation="Neither"
Padding="0"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}">
// Place my form elements here. In my case, I use Grid
</ScrollView>
</RelativeLayout>
Answered By - Sam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.