2017-12-19 13 views
0

Xamarinフォーム(バージョン2.5.0.121934)を使用して、Android、iOS、およびUWPをターゲットとするアプリケーションを開発中です。カスタムレンダラーが必要なテキストに下線と取り消し線を追加する必要があります。 AndroidとiOSの場合、すべて正常に動作しており、UWPではの取り消し線または下線が正しく適用されますが、これらの装飾は削除されません。ここでXamarinフォームのTextDecorationをクリアできません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; 
      } 
     } 
    } 
} 

EnhancedLabelXamarin.Forms.Labelを拡張し、取り消し線または下線を指定する簡単なBindablePropertyフィールドを追加する単純なクラスです。

レンダラーは正しくTextDecorations.Noneに設定されていますが、UIには影響しません。私はデバッガでこれを処理しましたが、ExtendedLabel内のTextBlockの状態はTextDecorations.Noneですが、実際には下線付きまたは取り消し線付きでUIが描画されています(どちらも追加できますが、除去される)。

私はXamarinのドキュメントを読んで、Bugzillaのバグを見てきましたが、手がかりは見つかりませんでした。他に誰かがこれに遭遇しましたか? UWP特有のコールがあるかどうかわかりません。私が逃したことをする必要があります。あるいは、TextDecorationsを使用してスタイルを適用する方法が間違っている場合や、実際にバグを見つけた場合です。

+0

友人は、https://stackoverflow.com/questions/16480344/programatically-removing-strikethrough-textdecoration-from-code-behind-in-wpfに私に指摘しました。 WPFのためにそこで働いたものは、UWPには適用されません。 – Ickster

答えて

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!" /> 

enter image description here

関連する問題