https://learn.microsoft.com/zh-cn/dotnet/communitytoolkit/maui/converters/bool-to-object-converter

using Microsoft.Maui.Media;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RG3.MauiAppHostCK.Converters
{
    /// <summary>
    /// 百分比zhuan转换器
    /// </summary>
    public class ValueLengthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string height && int.TryParse(parameter.ToString(), out int percentage))
            {
                if (height.Length > percentage) return $"{value.ToString().Substring(0, percentage)}...";
                return value;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string height && int.TryParse(parameter.ToString(), out int percentage))
            {
                if (height.Length > percentage) return $"{value.ToString().Substring(0, percentage)}...";
                return value;
            }
            return value;
        }
    }
}

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="RG3.MauiAppHostCK.MainPage"        
             xmlns:local="clr-namespace:RG3.MauiAppHostCK.Converters"
             Shell.NavBarIsVisible="False" Shell.TabBarIsVisible="False" BackgroundImageSource="banner_pdetail.png" >


  <ContentPage.Resources>
        <local:ValueLengthConverter x:Key = "valueLengthConverter" />
    </ContentPage.Resources>
 Text="{Binding RepentanceWord, Converter={StaticResource valueLengthConverter}, ConverterParameter='30'}" 
 <Label 
                                HeightRequest="90"
                                TextColor="#BD3124"
                                HorizontalTextAlignment="Center"
                                VerticalTextAlignment="Center"
                                HorizontalOptions="Center"
                                Text="{Binding RepentanceWord, Converter={StaticResource valueLengthConverter}, ConverterParameter='22'}" 
                                FontSize="30"
                                FontAttributes="None"
                                FontFamily="PingFangSC-regular" >
            <Label.Shadow>
                <Shadow Brush="#010000"
                                Offset="4,4"
                                Radius="4"
                                Opacity="0.6" />
            </Label.Shadow>
            <Label.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding NextWordRelayCommand}" 
                                                              Buttons="Primary,Secondary" 
                                                              CommandParameter="1"
                                        ></TapGestureRecognizer>
            </Label.GestureRecognizers>
        </Label>
文档更新时间: 2023-06-22 20:33   作者:admin