gpt4 book ai didi

c# - 根据文本需要多少空间自动设置 Label HeightRequest

转载 作者:行者123 更新时间:2023-12-04 16:47:01 24 4
gpt4 key购买 nike

我在 Frame 内的 StackLayout 内有一个 Label:

<Frame Grid.Row="0" Margin="0, 0, -10, 0" OutlineColor="Transparent" HasShadow="False" Grid.Column="1" BackgroundColor="{StaticResource rightBubbleColor}">
<StackLayout>
<Label
Style="{StaticResource rightBubbleStyle}"
TextColor="{StaticResource rightBubbleFontColor}"
Text="{Binding message}" />
</StackLayout>
</Frame>

这给了我这个结果:

Label without HeightRequest

这里的问题是最后一条消息实际上有更多的文本,需要 6 行才能显示整个文本。

如果我为 Label 设置 HeightRequest 的值,它也会更改 Frame 的高度。例如,如果我将值设置为 150,我会得到以下结果:

Label with HeightRequest

我需要根据 Text 需要的高度为 HeightRequest 动态设置一个值。

有谁知道我该如何实现(最好是在 xaml 中),或者这对我的问题来说是一种错误的方法吗?

编辑 1:

Label 中删除样式并不能解决问题。它只发生在 UWP 上。请注意,我没有 Windows Phone 来测试它,问题出现在我的本地计算机(Windows 10 桌面)上。

编辑2:

根据Xamarin documentation ,高度应该根据内容自动调整:

When the app developer sets the ListView.HasUnevenRows property to true, the behavior of the list view still depends on the ListView.RowHeight property. First, if the developer either does not set the ListView.RowHeight property or sets it to -1, list view items are autosized to fit their contents. This is the desired behavior and the intended use case for a ListView.HasUnevenRows value of true, as noted above.

最佳答案

我找到了一个解决方案。这是问题所在:

<RowDefinition Height="*" />

我将值设置为 Auto,现在可以正常使用了。奇怪的是它在 iOS 和 Android 上有不同的行为。

关于c# - 根据文本需要多少空间自动设置 Label HeightRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40421257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com