Plugin.XamarinForms.Converters
Cross platform library containing a bunch of XAML converters for Xamarin Forms.
Supported platforms
Name | Tested |
---|---|
Android | Yes |
iOS | Yes |
Setup
Install this package into your PCL/.NetStandard project. It does not require additional configurations.
Using the package
First, you just need to add the reference in your XAML file:
xmlns:conv="clr-namespace:Plugin.XamarinForms.Converters;assembly=Plugin.XamarinForms.Converters"
xmlns:enum="clr-namespace:Demo.Enums"
And then you can use it on this way:
<Entry Text="{Binding Text, Mode=TwoWay}" TextColor="DimGray" Placeholder="Enter a text" Keyboard="Text" />
<Label Text="{Binding Text, Converter={conv:ToLowerCaseConverter}}" />
<Label Text="{Binding Text, Converter={conv:SubstringConverter, MaxLenght=35}}" />
<Entry Text="{Binding Number, Mode=TwoWay, Converter={conv:EmptyToZeroConverter}}" Keyboard="Numeric" />
<Picker ItemDisplayBinding="{Binding ., Converter={conv:EnumDescriptionConverter}}"
SelectedItem="{Binding EventType, Mode=TwoWay}">
<Picker.ItemsSource>
<x:Array Type="{x:Type enum:EventType}">
<enum:EventType>None</enum:EventType>
<enum:EventType>Movie</enum:EventType>
<enum:EventType>Concert</enum:EventType>
<enum:EventType>Sports</enum:EventType>
<enum:EventType>CityTour</enum:EventType>
</x:Array>
</Picker.ItemsSource>
</Picker>
<Label Text="{Binding YourBooleanValue, Converter={conv:BoolToObjectConverter IfTrue='Represents True', IfFalse=500}}" />
Enum declaration:
public enum EventType
{
None,
Movie,
Concert,
Sports,
[Description("City Tour")]
CityTour
}
There are more useful converters in this package you can use
-
General
- EqualsConverter (requires additional property)
- InvertedBoolConverter
- IsNotNullConverter
- IsNullConverter
- EnumDescriptionConverter
- BoolToObjectConverter [Read more] (requires additional properties)
-
Image
- ByteArrayToImageConverter [Read more]
-
Number
- EmptyToNullNumberConverter [Read more]
- EmptyToZeroConverter [Read more] (do not use for nullable properties)
- IsPositiveConverter
- IsNegativeConverter
- IsNonPositiveConverter
- IsNonNegativeConverter
- IsLesserThanConverter (requires additional property)
- IsLesserOrEqualThanConverter (requires additional property)
- IsGreaterThanConverter (requires additional property)
- IsGreaterOrEqualThanConverter (requires additional property)
- IsInRangeConverter (requires additional properties)
-
String
- SubstringConverter [Read more] (optional property)
- ToLowerCaseConverter
- ToUpperCaseConverterer
- IsNonNullOrWhitespaceConverter A collaboration from [ronymesquita]
More examples
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Is equal to 10.5: " />
<Span.Text>
<Binding Text="{Binding Number, Converter={conv:EqualsConverter CompareTo=10.0}}" />
</Span.Text>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Is non negative: " />
<Span Text="{Binding YourNumber, Converter={conv:IsNonNegativeConverter}}" />
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Is Null: " />
<Span Text="{Binding YourNulleableObject, Converter={conv:IsNullConverter}}" />
</FormattedString>
</Label.FormattedText>
</Label>
Detailed information
BoolToObjectConverter
This converter defines two properties IfTrue
and IfFalse
of type object
. These properties represent the values you want to be returned depending on the boolean value of the bindable property you are using.
ByteArrayToImageConverter
For some reason I was required once to retreive byte array from images. After a couple of hours looking for a solution and trying out some approaches I found at Stack Overflow I realized there wasn't an easy way to do it. So I decided to keep my ItemsSource
as byte[]
and then use a converter to bind it in my XAML. So that's it.
EmptyTo_Converter
When binding an entry to a numeric property, deleting entry's text on the UI doesn't update the target property.
Ex: If the entry value is 123
and you start deleting, in some point the value will be 1
for both UI and view model. If you continue deleting the text on the UI will be an empty string however, binded property in view model stills 1
. By using these converters if you clear the entry, your binded property in view model will be null
or 0
, depending of which converter is been used.
SubstringConverter
Truncates the input string to the length provided in ConverterParameter
or to 50 characters if no value was provided. Also appends three dots if input's lenght is greater than provided length.