How to create multiple custom renderer with same type?

124 views Asked by At

I wanted to create a page render with ContentPage type. I created so in android and it is working but in IOS there has custom page renderer with same type (ContentPage). It can be removed as this was from nuget package and working on different context.

Here is my code

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace AlwinInvoiceGenerator.IOS
{
   using CoreGraphics;
   using UIKit;
   using Xamarin.Forms;
   using Xamarin.Forms.Platform.iOS;

public class CustomPageRenderer : PageRenderer
{
    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        if (Element == null || !(Element is ContentPage basePage) || basePage.BindingContext == null ||
            !(basePage.BindingContext is BaseViewModel baseViewModel))
        {
            return;
        }

        SetCustomBackButton(baseViewModel);
    }

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
    }

    private void SetCustomBackButton(BaseViewModel baseViewModel)
    {
        var fakeButton = new UIButton {Frame = new CGRect(0, 0, 50, 40), BackgroundColor = UIColor.Clear};
        fakeButton.TouchDown += (sender, e) =>
        {
            baseViewModel.OnBackButtonClicked();
        };

        NavigationController?.NavigationBar.AddSubview(fakeButton);
    }
}

It seems it not registering and that is why not calling. I have another page renderer that is register in assembly

[assembly: ExportRenderer(typeof(ContentPage), typeof(IOSToolbarExtensions.iOS.Renderers.IOSToolbarExtensionsContentPageRenderer), Priority = short.MaxValue)]

If I removed this line then above code is working but not two in the same time. Please help

1

There are 1 answers

0
NixA On BEST ANSWER

Same type seems not working for multiple renderer. I have created sub type of my custom renderer and override the methods which needed to. It is working well