[英]insert background image in a grid, that fits the size with Xamarin forms pcl
[英]how to insert a background image in a grid of Xamarin forms pcl
我需要將背景放置到網格(PNG圖像)中。 我無法在線找到具體的解決方案,您可以從xaml中添加它嗎? 還是必須通過c#采取行動?
謝謝。
<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>
您可以將其添加為Grid的子級,並添加Grid.ColumnSpan和Grid.RowSpan屬性以覆蓋所有網格。 例如:
<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>
(另外,如果您的網格位於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>
您可以為此使用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>
Xamarin Forms XAML上沒有ImageBrush,也沒有gridView ..只是類似的Grid布局,目前,它僅接受背景色
要添加圖像,您應該執行以下操作:
<Grid>
<Image Source="imgBackground.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>
</Grid>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.