Giter Club home page Giter Club logo

plugin.xamarinforms.converters's Introduction

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}, ConverterParameter=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 (required parameter)
    • InvertedBoolConverter
    • IsNotNullConverter
    • IsNullConverter
    • EnumDescriptionConverter
    • BoolToObjectConverter [Read more] (requires additional properties)
  • Image

  • Number

    • EmptyToNullNumberConverter [Read more]
    • EmptyToZeroConverter [Read more] (do not use for nullable properties)
    • IsPositiveConverter
    • IsNegativeConverter
    • IsNonPositiveConverter
    • IsNonNegativeConverter
    • IsLesserThanConverter (required parameter)
    • IsLesserOrEqualThanConverter (required parameter)
    • IsGreaterThanConverter (required parameter)
    • IsGreaterOrEqualThanConverter (required parameter)
  • String

    • SubstringConverter [Read more] (optional parameter)
    • ToLowerCaseConverter
    • ToUpperCaseConverterer

More examples

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Is equal to 10.5: " />
            <Span.Text>
                <Binding Path="Number" Converter="{conv:EqualsConverter}"> 
                    <Binding.ConverterParameter>
                        <x:Double>10.5</x:Double>
                    </Binding.ConverterParameter>
                </Binding>
            </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.

Good luck!

plugin.xamarinforms.converters's People

Contributors

ronymesquita avatar saimel avatar tranb3r avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.