Xamarin forms to .Net MAUI renderer conversion

92 views Asked by At

I am converting Xamarin renderer to .Net MAUI handler. I have custom Grid control as below DataGrid.xaml

<Grid
    <AbsoluteLayout Grid.Row="1">
        <ScrollView x:Name="Sv" Orientation="Horizontal" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
            <helpers:DataGridListView x:Name="ListView"></helpers:DataGridListView>
        </ScrollView>
        <helpers:DataGridVertScrollBar IsVisible="{Binding EnableCustomScroll}" x:Name="ssv" HeightRequest="100" WidthRequest="8" AbsoluteLayout.LayoutFlags="HeightProportional,YProportional,XProportional"
                 AbsoluteLayout.LayoutBounds="1,1,8,1"></helpers:DataGridVertScrollBar>
    </AbsoluteLayout>
    <ContentView x:Name="NoDataViewElement" Grid.Row="0" Grid.RowSpan="2" VerticalOptions="Center" IsVisible="False"/>
</Grid>

Helper class

namespace Test.Helpers.DataGrid
{
   public class DataGridListView :ListView
    {
        public Action<double>? OnScroll;

    }
}

Xamarin renderer as below

using System.Linq;
using Android.Content;
using Android.Widget;
using Test.Droid.Helpers;
using Test.Helpers.DataGrid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(DataGridListView), typeof(DGListViewRenderer))]

namespace Test.Droid.Helpers
{
    public class DGListViewRenderer : ListViewRenderer
    {
        double k = 1;
        DataGridVertScrollBar Sb;

        public DGListViewRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
        {
            base.OnElementChanged(e);
            Control.ScrollChange += Control_ScrollChange;
        }

        private void Control_Scroll(object sender, AbsListView.ScrollEventArgs e)
        {
            var list = (Android.Widget.ListView)sender;
            Move(sender);
        }

        private void Control_ScrollStateChanged(object sender, AbsListView.ScrollStateChangedEventArgs e)
        {
            var list = (Android.Widget.ListView)sender;
            Move(sender);
        }

        private void Control_ScrollChange(object sender, ScrollChangeEventArgs e)
        {
            Move(sender);
        }

Converting above renderer to .net MAUI handler as below

using Test.Helpers.DataGrid.Helpers;
using View = Android.Views.View;
using Microsoft.Maui.Handlers;
using Test.Helpers.DataGrid;

namespace Test.Platforms.Android.Handlers
{
    public partial class DGListViewHandler : ViewHandler<DataGridListView, View>
    {
        double k = 1;
        DataGridVertScrollBar Sb;

        public DGListViewHandler() : base(DGListViewMapper) { }
        public DGListViewHandler(IPropertyMapper mapper, CommandMapper? commandMapper = null) : base(mapper, commandMapper) { }

        public static PropertyMapper<DataGrid, DGListViewHandler> DGListViewMapper = new(ViewHandler.ViewMapper)
        {
            
        };

        private static void Control_ScrollChange(DGListViewHandler handler, DataGrid grid)
        {
            //throw new NotImplementedException();
        }

        protected override View CreatePlatformView() {
            return new View(Context);
        }
        protected override void ConnectHandler(View nativeView)
        {
            base.ConnectHandler(nativeView);
            if (VirtualView!= null)// Specified cast is invalid error
            {
                VirtualView.OnScroll += OnScroll;
            }
        }

        private void OnScroll(double obj)
        {
            throw new NotImplementedException();
        }

        private void Control_ScrollChange(object? sender, View.ScrollChangeEventArgs e)
        {
            Move(sender);
        }
        
        protected override void DisconnectHandler(View platformView)
        {
            base.DisconnectHandler(platformView);
        }
    }
}

In MauiProgrm.cs file

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
        })
        .ConfigureMauiHandlers((handlers) => {            
            handlers.AddHandler(typeof(DataGrid), typeof(DGListViewHandler));
        });

I receive error

if (VirtualView!= null)// Specified cast is invalid error

Also blank page instead of custom grid if I comment out above line

I am new to MAUi and no proper example for ListView handler Please help what is going wrong in my code Thanks in advance

I have also tried reusing the same renderer in MAUI application following the microsft doc But that also didn't work.

1

There are 1 answers

1
mpasdanis On BEST ANSWER

This error occurs because you're trying to add a handler for DataGrid, but your handler is actually for DataGridListView. The type specified in AddHandler must match the type your handler is designed to work with.

handlers.AddHandler(typeof(DataGridListView), typeof(DGListViewHandler));

Ensure that you're adding the handler for the correct type (DataGridListView in this case).