0

BadgeView在 Xamarin.Forms 中使用插件,TitleView但出现异常

Xamarin.Forms.Xaml.XamlParseException:位置 8:81。找到多个名称为“BadgeView.Shared.CircleView.CornerRadius”的属性。

这是我的代码

<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>
4

1 回答 1

1

这是插件中的一个错误,在插件的Github的问题部分中标记

并且有一个人添加了一种解决方法,但我不确定这是否对您有用,如此处所示所做的是

CircleView.cs

注意:注释 CornerRadius 属性。

    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); }
    //}
}

对于安卓:

CircleViewRenderer.cs

注意:为 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();
    }
}

对于 iOS:CircleViewRenderer.cs 注意:为 CornerRadius 添加硬编码值 (16) 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;
       Layer.CornerRadius = (float)(16) / 2.0f;
   }
}
于 2019-03-18T16:05:50.123 回答