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

标签 c# xaml xamarin height uwp

我在 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/

相关文章:

c# - 双向绑定(bind) : editing passed value from XAML control in the model setter does not update control

c# - 如何从 TimeSpan 中删除秒数?

c# - Xamarin NUnitLite 测试方法 : How to do shared tests that run in every platform?

c# - 安卓/Xamarin : How to get the label value of current activity?

c# - 无法加载类型 'WebApplication1.SiteMaster'

c# - Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} ) 有什么用?

wpf - 如何覆盖我的 ListBox 的 ItemTemplate 并仍然保留 DisplayMemberPath?

c++ - 如何获取屏幕的尺寸?

c# - 在 EFCore 中的单个 LINQ 查询中获得 2 个前 5 个

javascript - 我怎样才能让我的标签点击另一个按钮