Search code examples
androideventsxamarinplatformcustom-view

xamarin form: Custom Renderer not get executed if Custom control in Shared library


I have two project in a solution: One is a library project and the later one is a main project of the application.

The library project contains: shared library project, android, and iOS similar to the main project for the application.

The library project will have all the common code i can reuse.

In the library project inside the shared library, I have a custom control extending the StackLayout and the platform specific will have the Renderer extending the custom control.

If I put all the code to the main project of the application, the Renderer did call and able to do touch event I have written for the control. On the other hand, if the code was moved to library project, the control no longer have the touch event coz the Renderer written in android part didn't get call.

Does anyone know how I can make the renderer get called?


Solution

  • You can not write custom renderer in a shared code unless you use preprocessor directives like

    #if __IOS__
        //your ios specific code
    #endif
    #if __ANDROID__
        //your android specific code   
    #endif
    

    Maybe you are extending wrong class, if you post your code we can help better; for example i wrote a code to change navigation bar background on android like this

    public class CustomNavigationRenderer : NavigationRenderer
    {
        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);
            //my own code
    

    I haveto extend Xamarin.Forms.Platform.Android.NavigationRenderer and override OnLayout method. Hope that helps you.