首页 > 解决方案 > 设置 RowDefinition 可以采用 Xamarin 应用程序的最大高度

问题描述

我试图让第二行根据内容动态缩放,但它应该只增长到一定高度。与聊天应用程序所做的类似,可扩展编辑器会根据其包含的文本进行扩展。我的问题是,如果文本真的足够高,它可能会使第一行不可见。

如何设置第二行的最大高度,以免第一行被遮挡?

内容页面.cs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:renderer="clr-namespace:Project.renderers"
             x:Class="Project.MainPage">
    <Grid VerticalOptions="FillAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView VerticalOptions="Fill" Grid.Row="0"/>
        <StackLayout Orientation="Horizontal" 
                     VerticalOptions="EndAndExpand" 
                     BackgroundColor="Green" 
                     HorizontalOptions="Fill" 
                     Grid.Row="1">
            <renderer:ExpandableEditor VerticalOptions="FillAndExpand"
                                       HorizontalOptions="Center" 
                                       MaxLength="3000" 
                                       AutoSize="TextChanges" 
                                       TextColor="Black"/>
        </StackLayout>
    </Grid>
</ContentPage>

ExpandableEditor.cs

public class ExpandableEditor : Editor
{
    public ExpandableEditor()
    {
        TextChanged += OnTextChanged;
    }

    ~ExpandableEditor()
    {
        TextChanged -= OnTextChanged;
    }

    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        InvalidateMeasure();
    }
}

标签: c#xamlxamarin.forms

解决方案


您不能为 RowDefinition 设置 MaximumHeight。您应该为放置在行中的 VisualElement 设置最大高度。你可以用MinimumHeightRequest属性来做到这一点。

注意:MinimumHeightRequest 将在您请求的高度和您请求的最小高度之间选择最小值。(来源

** 更新 **

大约 3 年前在 StackOverflow 中提出了类似的问题。


推荐阅读