使用Xamarin Forms(版本2.5.0.121934),我正在开发一款针对Android,iOS和UWP的应用 . 我需要在某些需要自定义渲染器的文本中添加下划线和删除线 . 对于Android和iOS,一切正常,在UWP上,应用删除线或下划线正常工作,但删除这些装饰是行不通的 .
这是整个UWP渲染器:
[assembly: ExportRenderer(typeof(EnhancedLabel), typeof(EnhancedLabelRenderer))]
namespace myApp.UWP
{
public class EnhancedLabelRenderer : LabelRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
var strikethrough = ((EnhancedLabel)sender).Strikethrough;
var underline = ((EnhancedLabel)sender).Underline;
if (strikethrough && underline)
{
Control.TextDecorations = TextDecorations.Strikethrough | TextDecorations.Underline;
}
else if (strikethrough)
{
Control.TextDecorations = TextDecorations.Strikethrough;
}
else if (underline)
{
Control.TextDecorations = TextDecorations.Underline;
}
else
{
Control.TextDecorations = TextDecorations.None;
}
}
}
}
EnhancedLabel
是一个扩展 Xamarin.Forms.Label
的简单类,并添加了指定删除线或下划线的简单 BindableProperty
字段 .
渲染器正在正确设置 TextDecorations.None
,但是在调试器中没有通过此工作,并且实际上可以看到 ExtendedLabel
中的 TextBlock
的状态具有 TextDecorations.None
,但UI仍然使用下划线或删除线(实际上,或者那些可以添加,但都不能删除) .
我发现了任何线索 . 有人遇到过这种情况么?想知道我是否需要进行特定于UWP的调用我错过了,或者如果使用 TextDecorations
是错误的方式来应用样式,或者我实际上偶然发现了一个错误 .
1 回答
如果你想使用TextDecorations,你可以使用Run实例打包装饰文本,如下所示 .
根据您的要求,我创建了一个可以直接使用的CustomLabel .
CustomLabel.cs
CustomLabelRenderer.cs
Usage