簡體   English   中英

Xamarin.Forms-視圖高度等於寬度

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM