问题
I am using BadgeView plugin in Xamarin.Forms with TitleView
but getting exception
Xamarin.Forms.Xaml.XamlParseException: Position 8:81. Multiple properties with name 'BadgeView.Shared.CircleView.CornerRadius' found.
This is my code
<NavigationPage.TitleView>
<StackLayout Orientation="Horizontal" VerticalOptions="End" Spacing="10">
<Image Source="bell.png" HorizontalOptions="Center" VerticalOptions="Center"></Image>
<badge:BadgeView Text="2" BadgeColor="Yellow" VerticalOptions="End" HorizontalOptions="Start"></badge:BadgeView>
</StackLayout>
</NavigationPage.TitleView>
回答1:
This is a bug in the plugin which is marked in the issues section in the plugin's Github
And there is a workaround that one guy has added but I am not sure if that would work for you, as shown here what he did was
CircleView.cs
Note: Comment the CornerRadius property.
public 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); }
//}
}
For Android:
CircleViewRenderer.cs
Note: Added hard-coded value for CornerRadius (16)
public class CircleViewRenderer : BoxRenderer
{
private float _cornerRadius;
private RectF _bounds;
private Path _path;
public CircleViewRenderer(Context context)
: base(context)
{
}
public static void Initialize() { }
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
base.OnElementChanged(e);
if (Element == null)
{
return;
}
var element = (CircleView)Element;
_cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, (float)16, 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();
}
}
For iOS: CircleViewRenderer.cs Note: Added hard-coded value for CornerRadius (16) iOS code is not tested yet.
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;
Layer.CornerRadius = (float)(16) / 2.0f;
}
}
来源:https://stackoverflow.com/questions/55225340/multiple-properties-with-name-badgeview-shared-circleview-cornerradius-found