Xamarin.Forms:MultiLineLabel在Android上不再起作用 [英] Xamarin.Forms: the MultiLineLabel doesn't longer work on Android
问题描述
在Xamarin.Forms项目中,我使用 MultiLineLabel 在1或2行上显示标题,具体取决于文本的长度.我基于的是 blog 来实现这一目标.
On my Xamarin.Forms project, I use a MultiLineLabel to display a title on 1 or 2 lines, depending the text length. I'm based on this blog to achieve this.
所以我有一个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 上,我保留了给定的渲染器:
on iOS, I've kept the given renderer:
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 上,我已自定义渲染器:
on Android I've customized the renderer:
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上运行良好:
Until I used Xamarin.Forms v.2.3.4.247
, this worked well on Android:
但是在更新到最新版本(Xamarin.Forms v.2.4.0.269-pre2
)之后,它不再能按预期工作:
But after having updated to the latest version (Xamarin.Forms v.2.4.0.269-pre2
), it doesn't longer work as expected:
我还尝试使用博客上提供的渲染器:
I also tried to use the renderer given on the blog:
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);
}
}
但是我没有得到预期的渲染:
But I didn't get the expected rendering:
您有什么解释吗?还是其他建议?在iOS上,效果很好.
Would you have any explanation? Or another suggestion? On iOS this works well.
推荐答案
但是在更新到最新版本(Xamarin.Forms v.2.4.0.269-pre2)之后,它不再能按预期工作:
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
,其中包含以下代码:
I've checked the source codes of Xamarin.Forms v.2.4.0.269-pre2
. In LabelRenderer
's OnElementChange
event, FormsTextView
's SetLineBreakMode
will be called which contains following codes:
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);
中不存在).
As you can see, if you use LineBreakMode.TailTruncation
, textView.SetMaxLines(1);
and textView.SetSingleLine(true);
will be called, which disable the multi line function.(In 2.3.4
textView.SetSingleLine(true);
doesn't exist).
解决方案:
要解决此问题,只需在渲染器中添加两行代码:
To fix the problem, you simply need to add two lines of codes in your renderer:
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);
}
}
这篇关于Xamarin.Forms:MultiLineLabel在Android上不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!