为了限制标签的最大行数并指示文本截断,我们使用MaxLines和控件LineBreakMode的属性Label。
这在 Xamarin.Forms 中工作正常,但在 .NET MAUI 中不起作用。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui_POC.MainPage">
<ScrollView>
<StackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to .NET Multi-platform App UI, Cute dot net bot waving hi to you!"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
MaxLines="2"
LineBreakMode="TailTruncation"
HorizontalOptions="Center" />
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Center" />
</StackLayout>
</ScrollView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
它应该显示Label两行,并在第二行末尾显示“...”,如下所示。但在 MAUI 中,Android 和 iOS 上的情况都不是这样。在 Android 中,它确实显示省略号,但不显示第二行。
如何解决这个问题?
小智 3
如果您为 WidthRequest 设置一个值,则可以使用 LineBreakMode 说明如果文本太长,将如何处理文本。如果文本不适合 WidthRequest 指定的空间,TailTruncation 将在文本末尾添加省略号(三个点)。例如:
<Label x:Name="lblCityName"
Text="{Binding CityName, Mode=OneWay}"
FontSize="24"
WidthRequest="260"
LineBreakMode="TailTruncation"
Margin="5,0,0,0"
Grid.Row="0"
Grid.Column="0"
/>
Run Code Online (Sandbox Code Playgroud)
LineBreakMode 的其他选项可以在这里找到:https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.linebreakmode ?view=net-maui-7.0
| 归档时间: |
|
| 查看次数: |
3736 次 |
| 最近记录: |