在xamarin.forms中将背景图像设置为相对布局

时间:2016-08-08 12:16:29

标签: xamarin.forms

如何将背景图像设置为xamarin.forms便携式项目中的相对布局?

我尝试过以下代码:

Xamal文件

 <RelativeLayout x:Name="Title">
    <Label Text="Hello Xamarin"
        TextColor="Black"
        FontSize="22"
        FontFamily="Tahoma"
        HorizontalOptions="Center"
        HorizontalTextAlignment="Center"
        RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width, Factor=0.03}"
        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Factor=0, Property=Y}"
     />

xaml.cs文件

public partial class MainApp : ContentPage
{

    public Image BGImage { get; set; }
    public MainApp()
    {
        this.BackgroundImage = "/Images/landing_page.jpg";
        BindingContext = this;
        InitializeComponent();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以将图像视图添加到相对布局,请参阅以下链接;

https://forums.xamarin.com/discussion/17794/image-as-background

答案 1 :(得分:-1)

如果要在Xamal项目中为整个页面添加XAML文件中的背景图像,请使用BackgroundImage属性。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:PhoneDailerDemo"
             x:Class="PhoneDailerDemo.MainPage"
             BackgroundImage="image3.jpg">

    <Label Text="Welcome to Xamarin Forms!" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />
    <StackLayout Padding="100">
       //..........
    </StackLayout>
</ContentPage>