How to extend existing custom renderer for a button?

MoritzMoMoritzMo USMember ✭✭
edited July 2018 in Xamarin.Forms

hi,
i want to extend/override an existing custom renderer for a button.

I have an android library class which has an existing custom renderer for buttons. Lets say for example, the existing one in the library is setting a blue background color.
Now in my custom renderer, i want to set the beackground color to red.

I tried to extend the already existing custom renderer, nothing happens. I tried to create complete new one, nothing happens. When i debug it, it always goes into the custom renderer of the library, never into my new renderer.

Any ideas?

Best regards,
Moritz.

Answers

  • SzbestaSzbesta Member ✭✭✭

    @MoritzMoldenhauer
    Can you share the code of your CustomRenderer as well as the one youre trying to extend(the base which is hit by the debugger). Kinda hard to figure out your problem without the code.

  • MoritzMMoritzM Member

    Here is the code from the common library project i created:
    [assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))] namespace Common.Android.Renderer { public class MyButtonRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { // set background blue } } }

    And here is the code from the android project:
    [assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))] namespace Example.Android.Renderer { public class MyButtonRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Button> e) { // set background red } } }

    The example project is referencing the common project.
    When i run it, i never step into the example renderer, only in the common renderer and the background color is always blue.

  • MoritzMoMoritzMo USMember ✭✭

    @Szbesta said:
    @MoritzMoldenhauer
    Can you share the code of your CustomRenderer as well as the one youre trying to extend(the base which is hit by the debugger). Kinda hard to figure out your problem without the code.

    Sorry, i did answer with wrong account, but you can see it above.

  • SzbestaSzbesta Member ✭✭✭

    To me it looks like the problem probably is in the target control of both renderers. You are referencing the same control to be rendered by both of them(typeof(MyButton)). In the common one you dont want to use just switch the control to something else.

  • MoritzMoMoritzMo USMember ✭✭

    @Szbesta said:
    To me it looks like the problem probably is in the target control of both renderers. You are referencing the same control to be rendered by both of them(typeof(MyButton)). In the common one you dont want to use just switch the control to something else.

    This is not working for me. If i do not provice a new custom renderer in my android project, it should always fall back to the one in the android class library (common). when i point not to this control, it will not help me. The control itself is defined in xamarin.forms pcl, where my views are.

Sign In or Register to comment.