System.InvalidCastException:指定的强制转换无效Xamarin

时间:2016-06-01 14:54:35

标签: android xamarin telerik

这是我的甜甜饼饼图标签自定义标签渲染器的代码,我得到这个例外,我不知道该怎么办,请帮忙

异常来自行CategoricalDataPointdataPoint =(CategoricalDataPoint)relatedLabelNode;

当我将其更改为CategoricalDataPoint dataPoint =时                 relatedLabelNode.JavaCast();我得到System.InvalidCastException:无法将'Com.Telerik.Widget.Chart.Engine.DataPoints.PieDataPoint'类型的实例转换为'com / telerik / widget / chart / engine / dataPoints / CategoricalDataPoint'。

这是我的代码

   class CustomLabelRenderer : BaseLabelRenderer
{
    private String labelFormat = "{0}";
    private TextPaint paint = new TextPaint();
    private Paint strokePaint = new Paint();
    private Paint fillPaint = new Paint();
    private float labelMargin = 10.0f;
    private float labelPadding = 20.0f;

    public CustomLabelRenderer(DoughnutSeries owner)
        : base(owner)
    {

        this.strokePaint.SetStyle(Paint.Style.Stroke);
        this.strokePaint.Color = Color.White;
        this.strokePaint.StrokeWidth = 2;

        this.fillPaint.Color = Color.ParseColor("#F5413F");
        this.paint.TextSize = 35.0f;
        this.paint.Color = Color.White;
    }

    public override void RenderLabel(Canvas canvas,
        Com.Telerik.Widget.Chart.Engine.ElementTree.ChartNode relatedLabelNode)
    {

        CategoricalDataPoint dataPoint = (CategoricalDataPoint)relatedLabelNode;

        RadRect dataPointSlot = dataPoint.LayoutSlot;
        Double val = dataPoint.Value;
        String labelText = String.Format(this.labelFormat, (int)val);

        StaticLayout textInfo = this.CreateTextInfo(labelText, dataPoint);
        this.RenderLabel(canvas, dataPointSlot, labelText, textInfo);
    }

    private StaticLayout CreateTextInfo(String labelText,
        CategoricalDataPoint dataPoint)
    {

        return new StaticLayout(labelText,
            0,
            labelText.Length,
            this.paint,
            (int)Math.Round((float)dataPoint.LayoutSlot.Width),
            Layout.Alignment.AlignCenter,
            1.0f,
            1.0f,
            false);
    }

    private void RenderLabel(Canvas canvas, RadRect dataPointSlot,
        String labelText, StaticLayout textBounds)
    {

        RectF labelBounds = new RectF();
        float height = textBounds.Height + this.labelPadding * 2;
        float top = (float)dataPointSlot.GetY() - this.labelMargin - height;

        labelBounds.Set(
            (float)dataPointSlot.GetX(),
            top,
            (float)dataPointSlot.Right,
            top + height);

        canvas.DrawRect(
            labelBounds.Left,
            labelBounds.Top,
            labelBounds.Right,
            labelBounds.Bottom,
            this.fillPaint);

        canvas.DrawRect(
            labelBounds.Left,
            labelBounds.Top,
            labelBounds.Right,
            labelBounds.Bottom,
            this.strokePaint);

        canvas.DrawText(
            labelText,
            (float)dataPointSlot.GetX() + (float)(dataPointSlot.Width / 2.0) -
                textBounds.GetLineWidth(0) / 2.0f,
            labelBounds.CenterY() + textBounds.GetLineBottom(0) -
                textBounds.GetLineBaseline(0),
            paint);
    }
}

} `

1 个答案:

答案 0 :(得分:0)

分类系列数据仅支持笛卡尔(X / Y)图表类型。饼图仅支持PieSeries,因此对于您所描述的图表节点只能是PieDataPoint。它永远不会是CategoricalDataPoint

饼图支持的系列类型:http://docs.telerik.com/devtools/xamarin/controls/chart/types/chart-types-pie-chart#supported-series 以下是cartesion图表支持的内容:http://docs.telerik.com/devtools/xamarin/controls/chart/types/chart-types-cartesian-chart#supported-series

http://docs.telerik.com/devtools/xamarin/controls/chart/chart-overview