0

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

NixA
  • 11
  • 7
  • you shouldn't try to create a custom renderer for an existing type - instead create your own custom sub-type – Jason May 06 '22 at 12:15
  • @Json, this is an existing project and has pretty well no of functional pages. So I need to create this without sub type as those need to be implemented all the pages. Is there anything simple to implement that? Thanks – NixA May 06 '22 at 12:49
  • That's logically impossible. EITHER you override renderer for BUILT-IN TYPE, thus affecting ALL uses (ONLY ONE custom renderer for that type), OR you make your own SUB-TYPE. If there were TWO custom renderers FOR THE SAME TYPE, there would be no way for Xamarin to know which to pick when. How could it be otherwise? **Whichever custom renderer you use fewer places, make a sub-type for that one.** – ToolmakerSteve May 08 '22 at 02:30
  • thanks @ToolmakerSteve, I have created sub type of my custom renderer and override the methods which needed to. It is working well – NixA May 10 '22 at 10:50

1 Answers1

1

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

NixA
  • 11
  • 7