[英]Xamarin.Forms - View height equals width
我正在尝试将本机Xamarin.iOS转换为Xamarin.Forms,并遇到一个简单的布局问题。 我想要实现的是一个类似红色网格的菜单:
因此,每个Gridelement的高度应等于 宽度,但我得到的只是这个:
我的Xaml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<mvvm:BaseContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mvvm="clr-namespace:SharpLibrary.Forms.Source.MVVM;assembly=SharpLibrary.Forms"
xmlns:cc="clr-namespace:Gorilla.Forms.Source.UI.CustomControls"
x:Class="Gorilla.Forms.Source.UI.Guest.GuestMainPage">
<ContentPage.Content>
<RelativeLayout
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
>
<Image Style="{StaticResource StandardBackgroundImage}" />
<ScrollView
RelativeLayout.WidthConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Height}"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<cc:GridMenuItem
x:Name="testBox"
Grid.Row="0" Grid.Column="0"
BackgroundColor="Red"
Title="Test"
Icon="Text" />
<Frame
x:Name="testFrame"
Grid.Row="0" Grid.Column="1"
BackgroundColor="Gray"
HeightRequest="{Binding WidthRequest}">
<Label Text="gdjfgzhg" />
</Frame>
</Grid>
</ScrollView>
</RelativeLayout>
</ContentPage.Content>
</mvvm:BaseContentPage>
我无法弄清楚我做错了什么,我还发现了一些线程 ,人们希望实现与我相同的目的,但并没有达到我的期望。
希望有人知道答案
我建议您根据需要使用以下布局:
<!-- No need to use AbsoluteLayout or Constraint-->
<Grid>
<Image Style="{StaticResource StandardBackgroundImage}" />
<ScrollView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- Width responsive dependending on Screen, and Width (not WidthRequest) equals (Binding with source equals to itself)-->
<cc:GridMenuItem x:Name="testBox"
Grid.Row="0" Grid.Column="0"
HeightRequest="{Binding Width, Source={x:Reference testBox}}"
BackgroundColor="Red"
Title="Test"
Icon="Text" />
<Frame x:Name="testFrame"
Grid.Row="0" Grid.Column="1"
BackgroundColor="Gray"
HeightRequest="{Binding Width, Source={x:Reference testFrame}}">
<Label Text="gdjfgzhg" />
</Frame>
</Grid>
<ScrollView>
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.