Search code examples
c#genericsxamarin.formscustom-renderer

How to create a Xamarin.Forms custom renderer, where the renderer base class uses generics?


I have a base class, which looks like the following

public class DialogSelectionItem<Tobject> : ViewCell

Now I want to attach a custom renderer to it:

[assembly: ExportRenderer(typeof(SomeApp.CustomRenderers.DialogSelectionItem<Tobject>), typeof(SomeApp.iOS.CustomRenderers.DialogSelectionItemRenderer))]
namespace SomeApp.iOS.CustomRenderers
{
    public class DialogSelectionItemRenderer : ViewCellRenderer
    {
        // some customizations
    }
}

The problem is

The type or namespace name 'Tobject' could not be found (are you missing a using directive or an assembly reference?)

I can use object instead, but then the custom renderer is never called.

Is there an option to get the correct type or use the generic? How should I define the ExportRenderer?


Solution

  • OK the solution is posted here.

    Base class:

    public class DialogSelectionItem : ViewCell
    {
        // nothing
    }
    
    public class DialogSelectionItem<Tobject> : DialogSelectionItem
    {
        // do something
    }
    

    View renderer:

    [assembly: ExportRenderer(typeof(SomeApp.CustomRenderers.DialogSelectionItem), typeof(SomeApp.iOS.CustomRenderers.DialogSelectionItemRenderer))]
    namespace SomeApp.iOS.CustomRenderers
    {
        public class DialogSelectionItemRenderer : ViewCellRenderer
        {
            // some customizations
        }
    }