Forum Xamarin.Forms

Custom control renderer inside a shared project (Visual Studio)

The Custom Renderer Tutorial is very handy, but according to it you should create a renderer inside the iOS and Android project itself. I was wondering if it is possible to keep the renderer code together in a Shared Project.

I understand that shared project should not contain any platform specific code, but maybe there is a way to define certain code regions that would be only for iOS or Android.

For example in visual studio you can create a #if DEBUG / #endif region that would render only in if you project is being built in debug mode. Maybe there is some notation that would help the compiler differentiate the targets. In this case I would have both renderer classes in the same file and keep the drawing logic in one place.


  • MitchMilamMitchMilam USMember ✭✭✭

    Not really. The custom renderer is a class unto itself and must be in the platform-specific project because that is where information about the native controls reside.

  • KvantalianiLashaKvantalianiLasha GEMember ✭✭
    edited August 2014

    Thank you Mitch for the answer.
    I played with it and finally decided to make it with conditional compilation symbols. I defined "ANDROID" constant for android project and "IOS" constant for iOS project. So I just use #if ANDROID/#endif and #if IOS/#endif blocks in the code.

    A file in a shared project, as I figured out, belongs to both projects. There is a drop down menu on the top left corner that allows you switch between Android and iOS project views (see attached photo). So by switching that views you can actually see what will happen for each of the project builds.

    This way I can keep both renderers in the same file.


  • MitchMilamMitchMilam USMember ✭✭✭

    There are actually symbols already defined for the environments. The start with ___

Sign In or Register to comment.