Now we all know how Xamarin allows your to override the renderering process of any given UI control from native level and do any sort of modifications through Custom Renderers.
Completely overriding Xamarin Forms Control in Custom Renderer?
It even allows you to completely override the whole Control that’s associated with Xamarin Forms type Control and replace it with a completely native type Control. Specifically in cases like where you override the Control property with SetNativeControl() method in your Custom Renderer.
But when you do that…
But what happens when you complete replace and override the associated default control for your renderer is that, you will lose the access to Events of that particular control.
To be specific…
Let’s take a look at the following Custom Renderer…
There you can see we are completely replacing the default Button Control associated with the Renderer with our own native Button control.
When you do that, since you’re completely replacing everything, you will lose access to all the events and properties associated with the default control. Such as Button Click, Touch Events and so on.
As in try to subscribe to your CustomButton’s default Clicked event, it won’t fire up during run time, since it been overridden by the native button control.
So how to handle events?
Yep in that case, we need to handle those events manually and map those necessary events from our Native button control to Xamarin Forms level properties and events.
Yep this is how you manually create an Event for your Custom Button.
And then upon the Native Button’s Click event we invoke this event from the Custom Renderer level. 🙂
And then at the run time it will fire up the ClickedCustom event for your Custom Button in response of the native button’s click.
So now you can subscribe to ClickedCustom event instead of default Clicked event in your Xamarin Forms code and get the response upon the custom button click.
Any other type of events and properties…
You could follow the same pattern for any other type of event you wish to handle, and even properties such as Text, Color and blah blah… 😉