【问题标题】:Unable to clear TextDecoration in Xamarin Forms UWP App无法清除 Xamarin Forms UWP App 中的 TextDecoration
【发布时间】:2018-06-02 07:24:47
【问题描述】:

使用 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 中的错误,但没有找到任何线索。有人遇到过这种情况么?想知道是否有我错过的特定于 UWP 的调用,或者使用 TextDecorations 是否是应用样式的错误方法,或者我是否真的偶然发现了一个错误。

【问题讨论】:

标签: xamarin xamarin.forms uwp


【解决方案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);
        }
    }
}

用法

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

【讨论】:

    【解决方案2】:

    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 使用 Inlines 和大概是 RichTextBlocks 就可以避免这些问题。

    【讨论】:

      猜你喜欢
      • 2017-11-24
      • 1970-01-01
      • 2016-12-02
      • 2018-09-07
      • 2018-06-30
      • 1970-01-01
      • 2018-11-06
      • 2018-02-01
      • 2017-09-22
      相关资源
      最近更新 更多