2017-06-21 14 views
0

私はXamarinフォームプロジェクト(PCL)でカスタムレンダラーを実装しようとしています。私はラベルの角を曲げようとしています。 問題はレンダリングされないということです。私は関連ファイルにデバッグを投げたが、ヒットしているがレンダリングされていないようだ。ここでカスタムレンダラーがXamarinフォームをレンダリングしない

は、ラベルのサブクラスです:

public class CurvedCornersLabel:Label 
{ 
    public static readonly BindableProperty CurvedCornerRadiusProperty = 
     BindableProperty.Create(
      nameof(CurvedCornerRadius), 
      typeof(double), 
      typeof(CurvedCornersLabel), 
      12.0); 

    public double CurvedCornerRadius 
    { 
     get { return (double)GetValue(CurvedCornerRadiusProperty); } 
     set { SetValue(CurvedCornerRadiusProperty, value); } 
    } 


    public static readonly BindableProperty CurvedBackgroundColorProperty = 
     BindableProperty.Create(
      nameof(CurvedCornerRadius), 
      typeof(Color), 
      typeof(CurvedCornersLabel), 
      Color.Default); 
    public Color CurvedBackgroundColor 
    { 
     get { return (Color)GetValue(CurvedBackgroundColorProperty); } 
     set { SetValue(CurvedBackgroundColorProperty, value); } 
    } 
} 

これは、Androidのレンダラーです:

[assembly: ExportRenderer(typeof(CurvedCornersLabel), typeof(CurvedCornerLabelRenderer))] 
namespace CarouselDemo.Droid 
{ 
    public class CurvedCornerLabelRenderer:LabelRenderer 
    { 
     private GradientDrawable _gradientBackground; 

     protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
     { 
      base.OnElementChanged(e); 

      var view = (CurvedCornersLabel)Element; 
      if (view == null) return; 

      // creating gradient drawable for the curved background 
      _gradientBackground = new GradientDrawable(); 
      _gradientBackground.SetShape(ShapeType.Rectangle); 
      _gradientBackground.SetColor(view.CurvedBackgroundColor.ToAndroid()); 

      // Thickness of the stroke line 
      _gradientBackground.SetStroke(4, view.CurvedBackgroundColor.ToAndroid()); 

      // Radius for the curves 
      _gradientBackground.SetCornerRadius(
       DpToPixels(this.Context, 
       Convert.ToSingle(view.CurvedCornerRadius))); 

      // set the background of the label 
      Control.SetBackground(_gradientBackground); 
     } 

     /// <summary> 
     /// Device Independent Pixels to Actual Pixles conversion 
     /// </summary> 
     /// <param name="context"></param> 
     /// <param name="valueInDp"></param> 
     /// <returns></returns> 
     public static float DpToPixels(Context context, float valueInDp) 
     { 
      DisplayMetrics metrics = context.Resources.DisplayMetrics; 
      return TypedValue.ApplyDimension(ComplexUnitType.Dip, valueInDp, metrics); 
     } 


    } 
} 

これはXAMLです:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="CarouselDemo.LabelViewDemo" 
     xmlns:mr="clr-namespace:MR.Gestures;assembly=MR.Gestures" 
     xmlns:local="clr-namespace:CarouselDemo;assembly=CarouselDemo" 
     Padding="10,20,10,10"> 


<StackLayout Padding="20"> 
    <Label Text="Card 1" 
      HeightRequest="100" 
      BackgroundColor="LightGoldenrodYellow" 
      x:Name="card1" 
      /> 

    <local:CurvedCornersLabel Text="Card 2" 
      HeightRequest="100" 
      BackgroundColor="LightBlue" 
      x:Name="card2" 
      Margin="0,-40,0,0" 
      CurvedCornerRadius="15"    
      ></local:CurvedCornersLabel> 
</StackLayout> 

</ContentPage> 

任意のアイデア?

+0

この保護されたオーバーライドではありません。OnElementChanged(ElementChangedEventArgs

+0

私はそうは思わない、それだけでエラーをスローするには、 – user3355961

+0

そして、エラーは? –

答えて

0

私は解決策を見つけました。最初に、カスタムレンダラーの背景色を赤に変更しました。これは、赤で丸みを帯びているが、オーバーレイに青い角のコーナーが表示されていました。 Xamlから「LightBlue」を削除してカスタムレンダラを使って色を設定するとすぐに機能しました。

関連する問題