2017-05-15 17 views
0

カスタムレンダラを作成して、バッジのようにラベルの後ろに円を作成しました。 :私はこのような試みたXamarin Round Labelカスタムレンダラが動作しない

CircleView.cs(PCL)

public partial class CircleView : BoxView 
    { 
     public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(double), typeof(CircleView), 0.0); 

     public double CornerRadius 
     { 
      get { return (double)GetValue(CornerRadiusProperty); } 
      set { SetValue(CornerRadiusProperty, value); } 
     } 

     public CircleView() 
     { 
      InitializeComponent(); 
     } 
    } 

CircleViewRenderer.cs(アンドロイド)

[assembly: ExportRenderer(typeof(CircleView), typeof(CircleViewRenderer))] 
namespace TestApp.Droid 
{ 
    public class CircleViewRenderer : BoxRenderer 
    { 
     private float _cornerRadius; 
     private RectF _bounds; 
     private Path _path; 
     protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) 
     { 
      base.OnElementChanged(e); 

      if (Element == null) 
      { 
       return; 
      } 
      var element = (CircleView)Element; 

      _cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)element.CornerRadius, Context.Resources.DisplayMetrics); 

     } 

     protected override void OnSizeChanged(int w, int h, int oldw, int oldh) 
     { 
      base.OnSizeChanged(w, h, oldw, oldh); 
      if (w != oldw && h != oldh) 
      { 
       _bounds = new RectF(0, 0, w, h); 
      } 

      _path = new Path(); 
      _path.Reset(); 
      _path.AddRoundRect(_bounds, _cornerRadius, _cornerRadius, Path.Direction.Cw); 
      _path.Close(); 
     } 

     public override void Draw(Canvas canvas) 
     { 
      canvas.Save(); 
      canvas.ClipPath(_path); 
      base.Draw(canvas); 
      canvas.Restore(); 
     } 
    } 
} 

CircleViewRenderer.cs(iOS版)XAMLで

[assembly: ExportRenderer(typeof(CircleView), typeof(CircleViewRenderer))] 
namespace TestApp.iOS 
{ 
    public class CircleViewRenderer : BoxRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) 
     { 
      base.OnElementChanged(e); 

      if (Element == null) 
       return; 

      Layer.MasksToBounds = true; 
      Layer.CornerRadius = (float)((CircleView)Element).CornerRadius/2.0f; 
     } 

    } 
} 

<Grid> 
<customRenderer:CircleView x:Name="BadgeCircle" HeightRequest="16" WidthRequest="16" CornerRadius="16" VerticalOptions="Center" HorizontalOptions="Center" /><Label x:Name="BadgeLabel" TextColor="White" VerticalOptions="Center" HorizontalOptions="Center" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" FontSize="10"/> 
</Grid> 

しかし、何も表示されません。 CircleViewで初期化エラーが表示されていたので、InitializeComponent(); ここには何が欠けていますか?

+0

あなたは 'のInitializeComponentを(コメントを外してみてもらえ可能である。この

XFShapeView

を使用します) 'プロジェクトをビルドしますか? –

+0

が動作していないとエラーが発生しました –

+0

プロジェクトをクリアして再構築してください。まだエラーがある場合は、基本的なデモを共有できますか? –

答えて

0

フレームを使用できると思います。 CornerRadiusプロパティが必要です。

そうでなければ、私は、これはあなたが

var box = new ShapeView 
{ 
    ShapeType = ShapeType.Box, 
    HeightRequest = 75, 
    WidthRequest = 75, 
    Color = Color.Navy, 
    HorizontalOptions = LayoutOptions.Center, 
    CornerRadius = 5, 
    BorderColor = Color.Red, 
    BorderWidth = 1f, 
    Content = new Label 
    { 
     Text = "Touch me!", 
     FontSize = Device.GetNamedSize(NamedSize.Micro, typeof (Label)), 
     TextColor = Color.White, 
     HorizontalOptions = LayoutOptions.Fill, 
     VerticalOptions = LayoutOptions.Fill, 
     VerticalTextAlignment = TextAlignment.Center, 
     HorizontalTextAlignment = TextAlignment.Center, 
    }, 
}; 

それを使用することができますどのように形状タイプが

public enum ShapeType 
    { 
     /// <summary> 
     /// A 4-sides shape (square/rectangle) - can have rounded corners 
     /// </summary> 
     Box, 
     /// <summary> 
     /// A circle shape with a radius equals to the minimum value between height &amp; width 
     /// </summary> 
     Circle, 
     /// <summary> 
     /// A star shape for which you can define the number of points and the radius ratio 
     /// </summary> 
     Star, 
     /// <summary> 
     /// A triangle shape - the appearance depends on the height/width ratio 
     /// </summary> 
     Triangle, 
     /// <summary> 
     /// An oval shape - the appearance depends on the height/width ratio 
     /// </summary> 
     Oval, 
     /// <summary> 
     /// A diamond shape - 4-sides - the same you can find in a card deck - the appearance depends on the height/width ratio 
     /// </summary> 
     Diamond, 
     /// <summary> 
     /// A heart shape - the appearance depends on the minimum value between height &amp; width 
     /// </summary> 
     Heart, 
     /// <summary> 
     /// A progress circle shape acting like a progress bar with a radius equals to the minimum value between height &amp; width 
     /// </summary> 
     ProgressCircle, 
     /// <summary> 
     /// A custom path shape defined by a list of points 
     /// </summary> 
     Path 
関連する問題