canvasmauidrawable

System.TypeLoadException when running Maui.Graphics Live on Android


I was running the app on Android fine until I added the curve functionality where I am using Microsoft.Maui.Graphics **IDrawable **with GraphicsView .

When I build the app on android I get this error

System.TypeLoadException: 'Could not resolve type with token 010001db from typeref (expected class 'Microsoft.Maui.Graphics.Platform.GraphicsExtensions' in assembly 'Microsoft.Maui.Graphics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null')'

on MauiProgram.cs

public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder //Exception starts here
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

        return builder.Build();
    }

WeithPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ProjectAlpha.Drawables"
             x:Class="ProjectAlpha.WeithPage"
             Title="WeithPage">
    <ContentPage.Resources>
        <local:WeightDatesCurve x:Key="curve"></local:WeightDatesCurve>
    </ContentPage.Resources>
    <ScrollView>
        <VerticalStackLayout>
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
        
          <GraphicsView Drawable="{StaticResource curve}"
                HeightRequest="{Binding HeightDevice}"
                WidthRequest="750" />
            </VerticalStackLayout>
        </ScrollView>
</ContentPage>

WeightDatesCurve.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using ProjectAlpha.Models;

namespace ProjectAlpha.Drawables
{
   
    public class WeightDatesCurve : IDrawable
    {
       static PictureModel[] picModels;
        public static DateTime[] dates;
        public static float[] weights;
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            //Assigning properties of the curve X and Y lines
            canvas.StrokeColor = Colors.Blue;
            canvas.StrokeSize = 2;
            //Staring Point (0,0)
            Point StartPoint = new Point(20, dirtyRect.Height - 35);
            //End point of each axis
            Point EndX = new Point(dirtyRect.Width - 20, dirtyRect.Height - 35) ;
            Point EndY = new Point(20, 20);
            
            //Drawing X and Y lines
            canvas.DrawLine(StartPoint, EndX);
            canvas.DrawLine(StartPoint, EndY);
            
            //Scale values for both axes
            double xScale = (EndX.X - StartPoint.X) / dates.Length;
            double yScale = 5;
            //List of Points
            List<Point> points = new List<Point>();
            for (int i =0;i < dates.Length;i++)
            {
                double PointX = StartPoint.X + (i * xScale);
                double PointY = StartPoint.Y - (weights[i] * yScale);
                //Adding the current point to list
                points.Add(new Point(PointX, PointY)); 
                //Points to write the text (points)
                Point dateText = new Point(PointX, StartPoint.Y );
                Point weightText = new Point(PointX, PointY + 10);
                //Draw text
                canvas.DrawString(dates[i].ToString("M"), (float)dateText.X, (float)dateText.Y, 80,80, HorizontalAlignment.Center, VerticalAlignment.Top);
                canvas.DrawString(weights[i].ToString(), (float)weightText.X, (float)weightText.Y, 80,80, HorizontalAlignment.Left, VerticalAlignment.Top);
            }
            //Drawing the path (curve)
            PathF path= new PathF();
            path.MoveTo(points[0]);
            for (int i=1;i<points.Count;i++)
            {
                path.LineTo(points[i]);
            }
       //     path.Close();
            canvas.StrokeColor = Colors.Green;
            canvas.DrawPath(path);
           // canvas.Scale(1, 5000);
        }
    }
}

**WeithPage.xaml.cs: **


using ProjectAlpha.Models;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using ProjectAlpha.Drawables;
namespace ProjectAlpha;

public partial class WeithPage : ContentPage
{

    public WeithPage(UserModel user, List<PictureModel> pictureList)
    {
        InitializeComponent();
        WeightDatesCurve.dates = new DateTime[pictureList.Count];
        WeightDatesCurve.weights = new float[pictureList.Count];
        int i = 0;
        // var HeightDevice = DeviceDisplay.MainDisplayInfo.Height;
        var HeightDevice = 500;
        HeightDevice = HeightDevice - (HeightDevice / 3);
        BindingContext = new
        {
            HeightDevice = HeightDevice
    };
        foreach (var picture in pictureList)
        {
           WeightDatesCurve.dates[i] = picture.picture_datetime;
           WeightDatesCurve.weights[i]=(float)picture.weight;
            i++;
        }
    }
}

I have tried removing the ScrollView, did not work, same error.

Also tried to remove DeviceDisplay.MainDisplayInfo.Height and also faced same error.

The thing is that when I run it on the windows emulator on VStudio it works perfectly but when building it, i get this weird error. I just hope it is not a library compatibility issue, I tried lots of libraries to draw this curve and only this one showed green flag.

Appreciate your help!


Solution

  • I solved the issue by upgrading to .net7.0 and upgrading Maui.Graphics to latest version as well.