Xamarin Android中在自定义视图中解析XML错误时出错

时间:2019-02-05 12:09:03

标签: xamarin.android

在这里,我从View类扩展了TransparentCircleView类,但无法在.axml文件中使用该View。 提供错误分析XML时出错:格式不正确(无效的令牌)FieldStar.Droid E:\ Projects \ FieldStar \ FieldStar.Droid \ Resources \ layout \ activity_home.axml 44

TransparentCircleView.cs

namespace FieldStar.Droid.Views
{

[Register("FieldStar/Droid/Views/TransparentCircleView")]
public class TransparentCircleView : View
{
    private readonly string DEFAULT_BACKGROUND_COLOR = "#ffffff";
    private readonly string DEFAULT_FOREGROUND_COLOR = "#000000";
    private Paint _backgroundPaint, _foregroundPaint;
    public TransparentCircleView(Context context) : base(context)

    {
        Init();
    }

    public TransparentCircleView(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        Init();
    }

    public TransparentCircleView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        Init();
    }

    public TransparentCircleView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
    {
        Init();

    }

    protected TransparentCircleView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {

    }

    protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);
        int radius = -1;
        if (Width < Height)
        {
            radius = (Width - PaddingLeft - PaddingRight) / 2;
        }
        else
        {
            radius = (Height - PaddingTop - PaddingBottom) / 2;
        }

        //Background Circle
        canvas.DrawRect(0, 0, Width, Height, _backgroundPaint);

        //Foreground Circle
        canvas.DrawCircle(Width / 2, Height / 2, radius,_foregroundPaint);
    }

    private void Init()
    {
        _backgroundPaint = new Paint();
        _backgroundPaint.AntiAlias = true;
        _backgroundPaint.SetStyle(Paint.Style.Fill);
        _backgroundPaint.Color = Color.ParseColor(DEFAULT_BACKGROUND_COLOR);

        //Foreground View
        _foregroundPaint = new Paint();
        _foregroundPaint.AntiAlias = true;
        _foregroundPaint.SetStyle(Paint.Style.Fill);
        _foregroundPaint.Color = Resources.GetColor(Android.Resource.Color.Transparent);
    }

    public void SetBackgroundColor(string color)
    {
        _backgroundPaint.Color = Color.ParseColor(color);
        Invalidate();
    }

    public void SetForegroundColor(string color)
    {
        _foregroundPaint.Color = Color.ParseColor(color);
        Invalidate();
    }
}

}

1 个答案:

答案 0 :(得分:0)

此错误的最常见原因不是确保组件的名称空间路径为小写。您说返回的错误的事实是“解析XML时出错:格式不正确(无效的令牌) FieldStar.Droid ”,这往往将其指向此问题。确保您按以下方式引用xml中的组件:

<fieldStar.droid.views.TransparentCircleView
.....
.....
/>