其他分享
首页 > 其他分享> > Xamarin.Forms:MultiLineLabel不再适用于Android

Xamarin.Forms:MultiLineLabel不再适用于Android

作者:互联网

在我的Xamarin.Forms项目中,我使用MultiLineLabel在1或2行显示标题,具体取决于文本长度.我基于这个blog来实现这一目标.

所以我有一个MultiLineLabel控件:

public class MultiLineLabel : Label
{
    private static int _defaultLineSetting = -1;

    public static readonly BindableProperty LinesProperty = BindableProperty.Create(nameof(Lines), typeof(int), typeof(MultiLineLabel), _defaultLineSetting);
    public int Lines
    {
        get { return (int)GetValue(LinesProperty); }
        set { SetValue(LinesProperty, value); }
    }
}

我使用2个渲染器:

>在iOS上,我保留了给定的渲染器:

public class CustomMultiLineLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        MultiLineLabel multiLineLabel = (MultiLineLabel)Element;

        if (multiLineLabel != null && multiLineLabel.Lines != -1)
            Control.Lines = multiLineLabel.Lines;

    }
}

>在Android上我已经自定义了渲染器:

public class CustomMultiLineLabelRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);

        MultiLineLabel multiLineLabel = (MultiLineLabel)Element;

        if (multiLineLabel != null && multiLineLabel.Lines != -1)
        {
            Control.Ellipsize = TextUtils.TruncateAt.End;
            Control.SetMaxLines(multiLineLabel.Lines);
        }
    }
}

我在XAML中使用这样的MultiLineLabel:

<StackLayout 
    Grid.Row="0"
    Spacing="0">

    <local:MultiLineLabel
        Margin="8,6,8,0"
        TextColor="{ DynamicResource InverseTextColor }"
        Text="{ Binding encart_titre }"
        FontSize="{ artina:OnOrientationDouble 
            Default=16,
            PortraitTablet=20,
            LandscapeTablet=20 }"
        LineBreakMode="TailTruncation"
        Lines="2"
        Grid.Column="0"
        BackgroundColor="Yellow"
    />
</StackLayout>

直到我使用Xamarin.Forms v.2.3.4.247,这在Android上运行良好:

screenshot before update

但在更新到最新版本(Xamarin.Forms v.2.4.0.269-pre2)后,它不再按预期工作:

screenshot after update

我还试图使用博客上给出的渲染器:

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
    base.OnElementChanged(e);

    MultiLineLabel multiLineLabel = (MultiLineLabel)Element;

    if (multiLineLabel != null && multiLineLabel.Lines != -1)
    {
        Control.SetSingleLine(false);
        Control.SetLines(multiLineLabel.Lines);
    }
}

但我没有得到预期的渲染:
screenshot after updater - alternative

你有什么解释吗?还是另一个建议?在iOS上这很好用.

解决方法:

But after having updated to the latest version (Xamarin.Forms v.2.4.0.269-pre2), it doesn’t longer work as expected:

原因:

我查看了Xamarin.Forms v.2.4.0.269-pre2的源代码.在LabelRenderer的OnElementChange事件中,将调用FormsTextView的SetLineBreakMode,其中包含以下代码:

public static void SetLineBreakMode(this TextView textView, LineBreakMode lineBreakMode)
{
    switch (lineBreakMode)
    {
        case LineBreakMode.NoWrap:
            textView.SetMaxLines(1);
            textView.SetSingleLine(true);
            textView.Ellipsize = null;
            break;
        case LineBreakMode.WordWrap:
            textView.Ellipsize = null;
            textView.SetMaxLines(100);
            textView.SetSingleLine(false);
            break;
        case LineBreakMode.CharacterWrap:
            textView.Ellipsize = null;
            textView.SetMaxLines(100);
            textView.SetSingleLine(false);
            break;
        case LineBreakMode.HeadTruncation:
            textView.SetMaxLines(1);
            textView.SetSingleLine(true);
            textView.Ellipsize = TextUtils.TruncateAt.Start;
            break;
        case LineBreakMode.TailTruncation:
            textView.SetMaxLines(1);
            textView.SetSingleLine(true);
            textView.Ellipsize = TextUtils.TruncateAt.End;
            break;
        case LineBreakMode.MiddleTruncation:
            textView.SetMaxLines(1);
            textView.SetSingleLine(true);
            textView.Ellipsize = TextUtils.TruncateAt.Middle;
            break;
    }
}

如您所见,如果您使用LineBreakMode.TailTruncation,则为textView.SetMaxLines(1);和textView.SetSingleLine(true);将被调用,这将禁用多行功能.(在2.3.4 textView.SetSingleLine(true);不存在).

解:

要解决此问题,您只需在渲染器中添加两行代码:

protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Label> e)
{
    base.OnElementChanged(e);
    MultiLineLabel multiLineLabel = (MultiLineLabel)Element;

    if (multiLineLabel != null && multiLineLabel.Lines != -1)
    {
        Control.SetSingleLine(false);
        Control.SetMaxLines(multiLineLabel.Lines);
        Control.SetLines(multiLineLabel.Lines);

    }
}

标签:android,label,multiline,xamarin-forms,custom-renderer
来源: https://codeday.me/bug/20190608/1196174.html