首页 文章

无法在Xamarin Forms UWP App中清除TextDecoration

提问于
浏览
0

使用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 回答

  • 1

    想知道我是否需要进行特定于UWP的调用我错过了,或者如果使用TextDecorations是错误的方式来应用样式,或者我实际上偶然发现了一个错误 .

    如果你想使用TextDecorations,你可以使用Run实例打包装饰文本,如下所示 .

    Underline ul = new Underline();
    ul.TextDecorations = TextDecorations.Strikethrough;
    Run r = new Run();
    r.Text = "Here is an underlined text";
    ul.Inlines.Add(r);
    MyTextBlock.Inlines.Add(ul);
    

    根据您的要求,我创建了一个可以直接使用的CustomLabel .

    CustomLabel.cs

    public class CustomLabel : Label
    {
        public static readonly BindableProperty DeckProperty = BindableProperty.Create(
         propertyName: "Deck",
         returnType: typeof(TextDeck),
         declaringType: typeof(CustomLabel),
         defaultValue: default(TextDeck));
        public TextDeck Deck
        {
            get { return (TextDeck) GetValue(DeckProperty); }
            set { SetValue(DeckProperty, value); }
        }
    }
    
    public enum TextDeck
    {
        None = 0,
        //
        // Summary:
        //     Underline is applied to the text.
        Underline = 1,
        //
        // Summary:
        //     Strikethrough is applied to the text.
        Strikethrough = 2
    }
    

    CustomLabelRenderer.cs

    public class CustomLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                var element = Element as CustomLabel;
                var underline = new Underline();
                var run = new Run();
                switch (element.Deck)
                {
                    case TextDeck.None:
                        underline.TextDecorations = TextDecorations.None;
                        break;
                    case TextDeck.Strikethrough:
                        underline.TextDecorations = TextDecorations.Strikethrough;
                        break;
                    case TextDeck.Underline:
                        underline.TextDecorations = TextDecorations.Underline;
                        break;
                }
                run.Text = element.Text;
                underline.Inlines.Add(run);
                Control.Inlines.Clear();
                Control.Inlines.Add(underline);
            }
        }
    }
    

    Usage

    <local:CustomLabel Deck="Underline" Text="Welcome to Xamarin.Forms!" />
    

    enter image description here

相关问题