0
votes

I'm facing some problems at the time of painting a circle with the SkiaSharp API in one of the children pages of a Tabbed Page in Xamarin Forms.

Although I get no errors during the building of the project, whenever I enter the page in which the circle is supposed to be drawn during the on device debugging, the application throws a "System.InvalidCastException: Specified cast is not valid" message and it crashes.

This is the XAML code of the Tabbed Page:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:me="clr-namespace:TestBth;assembly=TestBth"
             x:Class="TestBth.MyTabbedPage">

    <TabbedPage.Children>

        <me:ConnectPage />
        <me:LissajousPage />
        <me:ParametersPage />

    </TabbedPage.Children>

    <!--Pages can be added as references or inline-->


</TabbedPage>

This is the XAML code of the page that's causing the trouble:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
             xmlns:local="clr-namespace:TestBth"
             x:Class="TestBth.LissajousPage"
             Title="Lissajous">

            <skia:SKCanvasView x:Name="canvasView"
                               PaintSurface="canvasView_PaintSurface" />

</ContentPage>

And this is the internal code of the same page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

using SkiaSharp;
using SkiaSharp.Views.Forms;

namespace TestBth
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LissajousPage : ContentPage
    {

        SKPaint blackFillPaint = new SKPaint
        {
            Style = SKPaintStyle.Fill,
            Color = SKColors.Black
        };

        public LissajousPage()
        {
            InitializeComponent();
        }


        private void canvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;

            canvas.Clear(SKColors.CornflowerBlue);

            int width = e.Info.Width;
            int height = e.Info.Height;

            //Set transforms
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(width / 200f);

            //Clock Background
            canvas.DrawCircle(0, 0, 100, blackFillPaint);
        }

    }
}

Any idea about why could this be happening?

Thanks in advance.

1
it should work , can you post output complete exceptions detailsHoussem
Have you bothered examining the stack trace?Paul Kertscher
It turns out the SkiaSharp.Views.Forms Nuget package was not installed for Android. I specified that I wanted to install it in all my projects but there must have been a problem during the installation. I installed the package individually for Android and it works fine now. Sorry for the inconvenience.I. Mendoza

1 Answers

1
votes

For my surprise, it turns out the SkiaSharp.Views.Forms Nuget package was not installed for Android.

I specified that I wanted to install it in all my projects but there must have been a problem during the installation.

I installed the package individually for Android and it works fine now.

Sorry for the inconvenience.