Delpin Susai Raj Tuesday 3 December 2019

Xamarin.Forms - Converter in MVVM using IValueConverter

In this blog post, you will learn how to use Converter in MVVM using IValueConverter in Xamarin.Forms.


Introduction

Xamarin.Forms - Working With Triggers
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.

Value Converter

Use Converter instead of Trigger. Data bindings usually transfer data from a source property to a target property, and in some cases from the target property to the source property. This transfer is straightforward when the source and target properties are of the same type, or when one type can be converted to the other type through an implicit conversion. When that is not the case, a type of conversion must take place.

Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.

Create a new or existing Xamarin forms(.Net standard) Project. With Android and iOS Platform.
Create IntToBoolConverer 

Now, Create an IntToBoolConverer class Derived from IValueConveter

I implemented IntToBoolConverter If value !=0 I return true or else false. Whatever logic you need you can implement here.

IntToBoolConverter.cs

Setting up the User Interface

Here, Use IntToBoolConverter in your XAML.

Add Namespace

Add Static Resource

MainPage.Xaml 

Click the "Play" button to try it out.

ValueToColorConveter

In this converter, I used to change color by given values.  If value = 0 Red color else Green color.

ValueToColorConveter.cs


Whatever you need to write conditions in XAML use Converter. My suggestion use converter instead of Triggers.

I hope you have understood how to use Converter in MVVM using IValueConverter in Xamarin.Forms.

Thanks for reading. Please share your comments and feedback. Happy Coding :)

5 comments:

  1. PDF format is the most effective format for studying and sharing. All we want is only a free Adobe reader, we are able to learn PDF with none issues. Nonetheless, do you understand that PDF will not be simple to edit? It is without doubt one of the key options of PDF format, however we do have to edit PDF information inevitably. If you want to learn more about this topic please visit https://onlineconvertfree.com/converter/video/

    ReplyDelete
  2. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
    icp-cat training

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Read this blog to get a better conclusion : PSM vs CSM

    ReplyDelete
  4. Thanks for sharing this informative content , Great work
    Read this Blog to become a perfect Product Manager : Best Books for Product managers

    ReplyDelete
  5. Join in my link now, this site can make you rich for a second
    lapak303 login
    dominobet

    ReplyDelete