Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - View height equals width

I'm trying to convert my native Xamarin.iOS to Xamarin.Forms and struggle with an easy layout problem. What I am trying to achieve is a Menu like this the red Grid:

Good Grid

So the height of each Gridelement should be equal the width but all I get is this one:

Wrong Grid

My Xaml looks like this:

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

I can't figure out what I do wrong, also I found some Threads where people wanted to achieve the same thing as I do, but it does not do what I expect.

Hope someone knows the answer

like image 335
DirtyNative Avatar asked Dec 18 '22 01:12

DirtyNative


1 Answers

I suggest you to use the following layout for your need:

    <!-- 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>
like image 127
Rudy Spano Avatar answered Dec 31 '22 16:12

Rudy Spano