【问题标题】:Setting the Maximum Height that a RowDefinition can take Xamarin App设置 RowDefinition 可以采用 Xamarin 应用程序的最大高度
【发布时间】:2019-02-25 17:54:00
【问题描述】:

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

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

ContentPage.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# xaml xamarin.forms


    【解决方案1】:

    您不能为 RowDefinition 设置 MaximumHeight。您应该为放置在行中的 VisualElement 设置最大高度。您可以使用 MinimumHeightRequest 属性执行此操作。

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

    ** 更新**

    A similar question 大约 3 年前在 StackOverflow 中被问到。

    【讨论】:

    • MinimumHeightRequest 来自属性名称本身,我认为这不是为了帮助您建议设置视觉元素的最大高度。
    • 没错,但这是 Xamarin.Forms 团队的错误。您可以在这些页面上获得有关 MinimumHeightRequest 的更多信息。 12
    • 设置 MinimumHeightRequest 仍然会使 ExpandableEditor 扩展到超出设置的大小。我将MinimumHeightRequest 设置为300...我实际上看到了这两个链接;但是,我在这里问它是因为它对我没有帮助。
    • 我认为 300 对于 MinimumHeightRequest 来说非常高。您尝试过较小的值吗?
    • 一开始我实际上已经将它设置为 50。现在,我将它设置为 20 并输入许多新行会导致第一行被覆盖。没有任何改变:(
    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多