Using ValueConverters in Xamarin.Forms XAML, Here Is How To Do It

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • ValueConverters can be a very powerful tool whenever you start working with Xamarin.Forms and XAML. You all probably ran into the situation where you got a weird value from your backend and needed to convert that into something else to show it on screen.
    In this video that is exactly what we will see. For the example I will turn a string value into a color by implementing a ValueConverter in XAML. We will also look into ConverterParameters briefly, although that doesn't seem to end very well...
    🛑 Don't forget to subscribe to my channel for more cool content: th-cam.com/users/GeraldVerslui...
    🔗 Links
    Sample code repo: github.com/jfversluis/XFValue...
    Basic Data Binding: • Basic Data Binding in ...
    Data Binding Scopes Video: • Xamarin.Forms ListView...
    Using Resources in Xamarin.Forms: • Using ResourceDictiona...
    Xamarin Forms Repo: github.com/xamarin/Xamarin.Forms
    Xamarin.Forms ValueConverters Docs: docs.microsoft.com/xamarin/xa...
    ⏱ Timestamps
    0:00 Intro
    0:33 Sample App Outline
    1:30 Implement Some UI
    2:15 Implement Sample Class
    2:39 Free Pro-Tip for Adding Properties!
    3:58 Implement More Sample Code (Data-Binding, Don't be Scared!)
    7:21 ValueConverter (String to Color) Implementation
    8:00 Implement IValueConverter Interface
    9:20 Implement Convert Method
    11:45 Add ValueConverter to XAML
    14:06 Run Sample App
    15:00 ConverterParameter with Binding (Doesn't Work! Epic Fail 🤦‍♂️)
    18:03 ConverterParameter with Static Value
    18:37 Outro
    🎥 Video edited with Camtasia (ref): techsmith.z6rjha.net/AJoeD
    🙋‍♂️ Also find my...
    Blog: blog.verslu.is
    Twitch: / jfversluis
    All the rest: jfversluis.dev
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 28

  • @jfversluis
    @jfversluis  3 ปีที่แล้ว +1

    O no! Demoes going wrong... What is your worst demo fail?

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Truth! Thanks Jony!

  • @florentwinamou6650
    @florentwinamou6650 3 ปีที่แล้ว +1

    thank you gerald for making xamarin so easy to understand...

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      So nice of you to say Florent! Really appreciate it!

  • @ralhamami
    @ralhamami 2 ปีที่แล้ว

    Awesome video, man. I like your style of explaining. Very easy to follow. 👍

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Glad you liked it! Let me know if there is something you need!

  • @issacproton7885
    @issacproton7885 3 ปีที่แล้ว +1

    Very Useful....now make a video about Creating Plugins from Scratch and Custom View Controls using SkiaSharp.

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +2

      They're on the list, thank you for your suggestions!

  • @hrishikeshgarud7177
    @hrishikeshgarud7177 2 ปีที่แล้ว

    Is it bad if i use only code behind in my application instead of MVVM? I find code behind very easy.

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว +1

      Absolutely not. Whatever works for you, keep doing that! There is no MVVM police that will come get you. Choosing MVVM will give you some advantages but if you decide you can live without that, that is fine too!

  • @jfras2359
    @jfras2359 2 ปีที่แล้ว +1

    Did you find out how to bind the ConverterParameter?

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Unfortunately it's just not possible. You will want to use MultiBinding: github.com/xamarin/Xamarin.Forms/issues/4565

  • @echolumaque2130
    @echolumaque2130 2 ปีที่แล้ว

    Hello Gerald, is there any workaround regarding binding the converter parameter? I'm stuck there too :(

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Why should there be a workaround for it? What is not working?

    • @kaspershudas2966
      @kaspershudas2966 2 ปีที่แล้ว

      @@jfversluis Hey, he probably meant this part: 15:00 ConverterParameter with Binding (Doesn't Work! Epic Fail 🤦‍♂)

  • @parsagachkar3140
    @parsagachkar3140 3 ปีที่แล้ว

    Did anyone found a way to use bindings for Value Converter Parameters? It's really weird even the official documentation does not specify a solution to achieve this :( I'm probably using the worst solution around by implementing it in a value converter (passing the entire View Model as the input and handling it inside the Value Converter).

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Isn't it described in detail in the Docs here? docs.microsoft.com/xamarin/xamarin-forms/app-fundamentals/data-binding/converters#binding-converter-parameters

    • @parsagachkar3140
      @parsagachkar3140 3 ปีที่แล้ว

      @@jfversluis thanks, unfortunately it just demonstrates binding to a static value of 255 I was talking about binding to a property or etc. The same part of video that failed. Looks like there is no way.

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Another way would be to create your own converter and add an additional (bindable) property?

    • @parsagachkar3140
      @parsagachkar3140 3 ปีที่แล้ว +1

      @@jfversluis actually it's a good idea. Thanks alot.

  • @rajeshjha3681
    @rajeshjha3681 3 ปีที่แล้ว +1

    ValueConverters are one of the things i hate about XAML. I always avoid using it.

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      O no Rajesh! Sorry to bring up traumas! Why do you hate them so much? And what do you do to avoid them?

    • @rajeshjha3681
      @rajeshjha3681 3 ปีที่แล้ว +1

      @@jfversluis I create a new property that simply performs the logic in the getter and then i bind it in XAML. It's a cheating but i prefer creating a new property than creating ValuConverters.

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      @@rajeshjha3681 That is one way to get around it for sure :) I don't know I feel my ViewModel is then getting cluttered with things it shouldn't be doing. But that's mostly personal preference.
      I hope my next video will be better for you! :D