Xamarin.Forms - 视图高度等于宽度

时间:2018-01-22 09:15:17

标签: c# xamarin.forms xamarin.ios

我试图将我的原生Xamarin.iOS转换为Xamarin.Forms并且遇到一个简单的布局问题。我想要实现的是像这样的菜单红色网格:

Good Grid

因此每个Gridelement的高度等于 宽度,但我得到的只是这一个:

Wrong Grid

我的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>

我无法弄清楚我做错了什么,我也发现了一些Threads人们希望与我做同样的事情,但它没有达到我的预期。

希望有人知道答案

1 个答案:

答案 0 :(得分:4)

我建议您根据需要使用以下布局:

    <!-- 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>