Creating a behavior to set state changes

With Behavior, we can add functionality to controls without having to subclass them. With behaviors, we can also create a more reusable code than we could if we subclassed a control. The more specific the Behavior we create, the more reusable it will be. For example, a Behavior that inherits from Behavior<View> could be used on all controls, but a Behavior that inherits from a Button can only be used for buttons. Because of this, we always want to create behaviors with a less specific base class.

When we create a Behavior, we need to override two methods: OnAttached and OnDetachingFrom. It is really important to remove event listeners in the OnDeattached method if we have added them in the OnAttached ...

Get Xamarin.Forms Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.