【问题标题】:Xamarin.Forms - View height equals widthXamarin.Forms - 视图高度等于宽度
【发布时间】:2018-01-22 20:51:30
【问题描述】:

我正在尝试将我的本机 Xamarin.iOS 转换为 Xamarin.Forms,并努力解决一个简单的布局问题。我想要实现的是像这样的红色网格菜单:

所以每个 Gridelement 的 height 应该 equal width 但我得到的只是这个:

我的 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,人们希望实现与我相同的目标,但它并没有达到我的预期。

希望有人知道答案

【问题讨论】:

    标签: c# xamarin.forms xamarin.ios


    【解决方案1】:

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

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

    【讨论】:

    • 完美运行!我还在 GridMenuItem 控件中定义了高度,这也有效!非常感谢!!
    猜你喜欢
    • 2014-11-11
    • 2016-08-09
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2015-11-14
    • 2010-11-08
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多