使用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
,但这不会对UI产生影响。我已经在调试器中完成了这项工作,实际上可以看到ExtendedLabel
中TextBlock
的状态为TextDecorations.None
,但UI仍然使用下划线或删除线来绘制它(本质上,这两种方式都可以添加,但都不能删除)。
我已经浏览了Xamarin文档并查看了Bugzilla中的bug,但没有找到任何线索。还有没有其他人遇到过这种情况?我想知道是否有一个特定于UWP的调用我错过了,或者使用TextDecorations
是不正确的应用样式的方式,或者我是否真的偶然发现了一个错误。
发布于 2019-06-11 00:19:46
UWP中的错误,如下面的Xaml中的:
<TextBlock>
<Run Text="Decorations can be toggled on and off"/>
</TextBlock>
<TextBlock Text="Decorations will not toggle off"/>
如果对TextBlock进行编码,也会出现同样的问题:
TextBlock textBlock = new TextBlock { FontSize = 18.0 };
textBlock.Inlines.Add(new Windows.UI.Xaml.Documents.Run { Text = "This text will not stick on text decoration." });
TextBlock textBlockBad = new TextBlock
{
FontSize = 18.0,
Text = "This text will not enable the TextDecorations to be turned off"
};
与Typography.Capitals相同的行为
只需要对TextBlocks和RichTextBlocks使用内联就可以避免这些问题。
https://stackoverflow.com/questions/47894094
复制相似问题