简体   繁体   English

如何在Xamarin表格pcl的网格中插入背景图像

[英]how to insert a background image in a grid of Xamarin forms pcl

I need to put a background to a grid, a PNG image. 我需要将背景放置到网格(PNG图像)中。 I can not find concrete solutions online, you can add it from xaml? 我无法在线找到具体的解决方案,您可以从xaml中添加它吗? or necessarily must act through c #? 还是必须通过c#采取行动?

Thank you. 谢谢。

    <StackLayout VerticalOptions="FillAndExpand" x:Name="allContent" HorizontalOptions="FillAndExpand" Orientation="Vertical" Spacing="0">    
<Grid Grid.Row="1">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="6*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
              </Grid.ColumnDefinitions>
              <Grid.RowDefinitions>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
              </Grid.RowDefinitions>
              <ActivityIndicator x:Name="loading" Grid.Row="0" Grid.Column="1" IsVisible="false" Color="#008ECC" IsRunning="true" />
              <Label TextColor="#fff" Grid.Row="1" Grid.Column="1" Text="" />
              <Entry FontSize="24" Grid.Row="1" Grid.Column="1" Placeholder="Username" x:Name="UsernameEntry" Text="" />
              <Label TextColor="#fff" Grid.Row="2" Grid.Column="1" Text="" />
              <Entry FontSize="24" Grid.Row="2" Grid.Column="1" Placeholder="Password" IsPassword="True" x:Name="PasswordEntry" Text="" />
              <Button x:Name="LoginButton" Grid.Row="3" Grid.Column="1" Text="Accedi" Clicked="Login_OnClicked"/>
              <Label TextColor="#fff" Text="Ricorda accesso" Grid.Row="3" Grid.Column="1"></Label>
              <Switch x:Name="switchRememberPassword" Grid.Row="4" Grid.Column="1"></Switch>              
              <Label x:Name="recuperaPassword" Grid.Row="4" Grid.Column="1" TextColor="#fff" Text="Recupera credenziali di accesso" FontSize="12"></Label>
            </Grid>
</StackLayout>

You can add it as a child of your Grid, and add the properties Grid.ColumnSpan and Grid.RowSpan to span to all your grid. 您可以将其添加为Grid的子级,并添加Grid.ColumnSpan和Grid.RowSpan属性以覆盖所有网格。 For example: 例如:

     <Grid>
        <Image HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" Source="" Grid.Row="6" Grid.Column="4" Grid.RowSpan="2" Grid.ColumnSpan="2"/>
            <!-- My other properties here code here-->
     </Grid>

(Also if your Grid is on a ContentPage and it occupies the full screen you can use the property BackgroundImage of your page) (另外,如果您的网格位于ContentPage上并且占据了全屏,则可以使用页面的BackgroundImage属性)

<RelativeLayout>
<Image Aspect="Fill" Source="Jupiter.png" Opacity="0.3"
            RelativeLayout.WidthConstraint=
              "{ConstraintExpression Type=RelativeToParent, Property=Width}"
            RelativeLayout.HeightConstraint=
              "{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
<Grid RelativeLayout.WidthConstraint=
          "{ConstraintExpression Type=RelativeToParent, Property=Width}"
        RelativeLayout.HeightConstraint=
          "{ConstraintExpression Type=RelativeToParent, Property=Height}">

  <Label Text="Hello world from XAML" VerticalOptions="Center"
     HorizontalOptions="Center" FontSize="30"/>
</Grid>

You can use RelativeLayout for this 您可以为此使用RelativeLayout

    <RelativeLayout>
       <Image Source="backgroundimage"   RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" 
     RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}" />
       <Grid RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}" 
     RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1}"> 

//Your grid content


       </Grid>
    </RelativeLayout>

There's no ImageBrush on Xamarin Forms XAML, also there's no gridView.. just the similar Grid layout, and for now, it's only accepting a background color Xamarin Forms XAML上没有ImageBrush,也没有gridView ..只是类似的Grid布局,目前,它仅接受背景色

to add a image you should do something like: 要添加图像,您应该执行以下操作:

<Grid>
<Image Source="imgBackground.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM