Converters in Bindy are used to convert values between different types. This is particularly useful when synchronizing values between Bindables with different data types.
How they work
Converters are compiled ahead of time (AOT) and a Converter Registry is used to get the proper converter to perform a conversion. The registry is populated with all the available converters at compile time via code generation.
When a Bindable receives a new value from the Bind it’s connected to, and the value’s type is different from the Bindable’s value type, the system automatically uses the Converter Registry to find a converter that can perform the required conversion.
Adding a new converter to the system is easy. Simply inherit from the IValueConverter interface, and then run a Bindy Refresh to trigger the code generation that registers the newly created converter to the Converter Registry.
The IValueConverter interface defines the methods required for implementing a new converter.
registerToConverterRegistry: This flag determines whether the converter should be registered to the converter registry and refresh the list of available converters. This is useful for special converters that are not registered to the converter registry by default.
sourceType: Gets the source type of the conversion.
targetType: Gets the target type of the conversion.
CanConvert(Type source, Type target): Determines whether the converter can convert between the specified source and target types.
Convert(object value, Type target): Converts the specified value to the target type.
Bindy comes with a variety of built-in converters that can be used out-of-the-box. These converters include:
In conclusion, Converters are a crucial component of the Bindy system, allowing for easy and flexible conversion of values between different types, thus enabling synchronization of values between Bindables with different data types.