Create Custom Controls using BindableProperty for Xamarin.Forms

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ส.ค. 2024
  • If you are looking to create custom controls for Xamarin.Forms, you have come to the right place. In this video we will learn how to implement BindableProperty on our own controls, making them reusable. You can then use it in your own project or maybe create an awesome control as a NuGet!
    🛑 Don't forget to subscribe to my channel for more cool content: th-cam.com/users/GeraldVerslui...
    🐦 Follow me on Twitter
    / jfversluis
    🔗 Links
    Sample code repo: github.com/jfversluis/XFBinda...
    Basic Data Binding: • Basic Data Binding in ...
    Data Binding Scopes Video: • Xamarin.Forms ListView...
    Data Binding Modes video: • Three Data-Binding Tri...
    BindableProperty Docs: docs.microsoft.com/xamarin/xa...
    Databinding Modes Docs: docs.microsoft.com/xamarin/xa...
    Xamarin Forms Repo: github.com/xamarin/Xamarin.Forms
    ⏱ Timestamps
    0:00 Intro
    0:30 Sample App Outline
    1:32 Implement Custom Control
    3:47 Implement BindableProperty
    7:00 Closer look at the BindableProperty.Create parameters
    9:00 Implement PropertyChanged to Update our Control
    12:49 Implement Second Bindable Property
    14:00 Consume Custom Control and Bind Properties
    16:27 Running the Sample
    17:12 Outro, Like & Subscribe!
    🎥 Video edited with Camtasia (ref): techsmith.z6rjha.net/AJoeD
    🙋‍♂️ Also find my...
    Twitter: / jfversluis
    Blog: blog.verslu.is
    Twitch: / jfversluis
    All the rest: jfversluis.dev
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Did you build custom controls, Nugets, anything? Post them here!

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

      I tried to do a CustomSwitchCell which is essentially a label and a switch in a stacklyaout, but unable to figure out how to do the two way binding correctly with the switch, though binding is working just fine with the label. My plan is to make a SwitchCell which is similar to the native switch cell but exposes more properties especially for the text part to allow for setting font size, attributes, type, ...etc. which is not available in the native switchcell.
      I wish if you can make a follow up video on how to do more complex controls. Thanks!

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

      Be aware that cells aren’t recommended anymore because the ListView is not the recommended was to show data anymore :)

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

      @@jfversluis I'm essentially going to use it in a settings page with a TableView. I agree that you can do the exact thing with CollectionView with full customizations, but that probably would require more work compared to a TableView with few SwitchCells :)

    • @Grand1Master
      @Grand1Master ปีที่แล้ว

      ​@@jfversluis just realised that Dependency injection doesn't work in custom controls when creating a view model for my control.. after googling found issue#4538 in maui issues. Adding here so anyone who stumbled like me might come here..

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

      Maybe I don’t understand but why would you want dependencies injected in controls? You should get the data through your view models and have your controls have bindable properties that you bind to

  • @jeffm3865
    @jeffm3865 2 ปีที่แล้ว +4

    This was very helpful. I could not find another useful tutorial on this topic, and your video was very straightforward.
    Would you consider a follow up video with a more complex user control?
    I’m trying to implement my own TreeView-like control in Xamarin /Maui. Each node would have a description and nested sub elements in a ObservableRangeCollection. I’m trying to implement this using MVVM, but this type of user control might require nested user controls, and a more complex control example might provide more insight.

  • @octagoncow
    @octagoncow ปีที่แล้ว

    Thank you for the clear and straightforward video! I was pulling my hair out trying to figure out bindable properties before watching this. I wish you would have done a different type of control other than another label for the second control but your instructions were clear enough to show that the process is basically the same regardless of the control type. Keep up the great work!

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

      Glad it was helpful! Thank you so much for your support! Appreciate it!

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

    Awesome, thank you! I really came to the right place ;) When I saw your video about Xamarin 5 I thought, control templates were the way to go :D.

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

      Haha I’m glad you feel that way! Enjoy the content!

  • @hrolfurgestsson8544
    @hrolfurgestsson8544 2 หลายเดือนก่อน

    This video made this a good Friday for me. I found the one-liner I was missing. Thank you sir.

    • @jfversluis
      @jfversluis  2 หลายเดือนก่อน

      Perfect! Have a great weekend!

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

    Yeeeeeey....finally it's here! Thanks a lot for the awesome content!

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

      Hope this is what you were looking for! Let me know if you have any questions :)

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

      @@jfversluis Sure,So much love and respect for you dear !

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

    I am falling in love with your tutorials

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

      Oelala well thank you! Don't tell my wife!

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

    Thanks for video!, I had many problems understanding code examples but with the video example I was able to do it in my code

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

      Great! Good luck and thanks so much for letting me know :)

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

    Thank you so much for this walk through! This is some serious voodoo magic.

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

      Glad to help! Yeah these things have a bit of magic in them 😅 let me know if you need anything else!

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

    very good thank you. I will use this in a next project

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

      Awesome! If you have something to share, please do!

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

    Thanks so much, This video is very helpful for me. Best regards.

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

      Thank you for letting me know! Appreciate it!

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

    Thanks for this!

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

      Glad it was helpful! My pleasure!

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

      @@jfversluis Rather than recreate each individual property, could you set the BindingContext of MyControl to `this` and just have it use the properties like your pages would?

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

    Awesome!😘

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

    Your contents are great!

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

      Thank you! :D

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

      @@jfversluis can you please make a video regarding messaging or chatting with another user each individual user implementation in Xamarin forms C# without using firebase

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

      That sounds more like building a complete app 😅 I don’t think I will have the time to get around to anything that extensive, sorry!

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

      @@jfversluis just an implementation of chat system using simple DB

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

    Yet another great tutorial! I would love to see an example of using picker or other complex controls. Also - I need some training on creating nuget packages - have done PR's and contributed to others - but don't know how to set my own up.

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

      Noted! You mean just using the Picker or other controls? Or in regard to the bindable properties?
      The NuGet one might be a nice one as well!

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

      @@jfversluis Picker is a good one - it doesn't have a easily defined backing object type like "string" or "uri". It is also confusing because binding an item source (in my case, building a state picker and country picker - both will have static itemsources) doesn't seem to work. Do I include those item sources in my control? Not sure how to do that in a custom control. It also has a selecteditemindex and selecteditem. If I bind the itemsource in my control, I want to expose only the selecteditem to my consuming page.

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

      In the project I am currently working on, I have three different kinds of contacts: Employees, Customers, and Vendors. All three have at least one Person associated (customers and vendors may have a number of Persons). I started using a template - but really like using a control instead (makes my Pages FAR more readable). I think this pattern is EXTREMELY common - so was thinking I would share with the community my "Person Control" as a nuget package

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

    Hey Gerald another great video. One quick question: Is it a good practice to bind the Title labels Text property to TitleTextProperty to decrease the amount of code?

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

      Thanks Polat! Ah directly bind to that, yeah I don't see why not? It might get a little unclear if you keep adding up the "magic" that way for other developers. But if that's what you want to do, sure :)

  • @woistdasniveau8290
    @woistdasniveau8290 ปีที่แล้ว

    A Video about custom attributes would also be nice. How to use the value of the variable the Attribute is on for example.

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

    Do you by chance have any examples where changes made to the value of say MyDescription is automatically propagated to the user control and automatically updated? I tried adding some buttons to the mainpage that changes the value of the MyDescription property but the DescriptionTextPropertyChanged event in my user control does not fire thus the value is not updated in my user control. I would love to see an example of this using an observable collection as a BindableProperty in my control that automatically responds to the changes made to the collection in the main page.

  • @mmeuwese
    @mmeuwese ปีที่แล้ว

    I don't understand how this could have ever properly worked. The DescriptionTextProperty is having the nameof(TitleText) bindableproperty? Will this still be valid for a 2 way bindingmode?

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

    Hi Gerald, is this still valid in .NET MAUI? Are there other/better ways to do that?

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

    Hi!
    This has to be one of the largest gray areas in Xamarin.Forms development, so thank you very much for the video. Have you ever considered creating a video for custom controls with Datatemplate? I'm trying to build a generic lookup control (I don't really like standard's picker user interace) where I can place a listview and by using generics, being able to interact with backend and filter results from list, etc... My main issue is on how do I handle listview item's layout customization... I know Datatemplate is the answer for that, but I can't figure out how to implement it.

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

      Hm that is a tough one to crack. Can't see I have implemented it like that yet. Let me know when you make some progress, I might be able to help or if you get it done make a video about it ;)

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

      @@jfversluis Ok, I'll take a shot at it... Seems to be a pretty useful thing right?

  • @AslamKhan-dh6to
    @AslamKhan-dh6to 3 ปีที่แล้ว

    Thank you so much for the video, how to pass object as bindable properties while implementing custom component ??

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

      I'm not sure what you mean

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

    Hey, thank you for the great tutorial. I'm new to Xamarin forms and i'm trying (using mvvm pattern) to dynamically create the controls.
    Do I have to do that with ContentView or is there any other way?

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

      Thanks Hoka! Appreciate the kind words!
      What do you mean by "dynamically create controls" exactly? Create the actual control or load the data? What is the use case you're trying to implement?

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

      @@jfversluis Thanks for the fast reply :) I got it to work though with the help of this video.
      My issue was that i have a JSON file/object with list of types and other attributes and depending on the type (button label entry etc.) I would create the control(s).

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

      @@hokaturki121 That sounds like an awesome complex thing! Great you made it work!

  • @fabioikomotta
    @fabioikomotta ปีที่แล้ว

    I follow your code and I can't directly Binding with viewmodel.
    The only way its works is using the followed code:
    Title="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:TestViewModel}}, Path=Text}"
    Direly Binding do not works:
    Title="{Binding Text}" do not works
    I'm using CommunityToolkit 8.0:
    [ObservableProperty]
    string text;
    Any idea?

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

    Great video! My custom control also needs an own viewmodel. Unfortunately, I can not get this to work. Any hints or ressources for that problem?

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

      Hey, thanks you! Not sure what you mean by this one though... Sorry :(

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

    Could you show this in the context of a button? So you can transition between two different states on an image or colour etc?

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

      I'm not entirely sure what you want to see here?

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

    Hello Gerald and thanks for your great work. I made a custom control with few color bindable properties. It works perfectly with normal bindings but when i try to use an "AppThemeBinding", i receive a Xaml error XFC0009...Do you have any suggestion?

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

      Hm I don't know from the top of my head what that error is. Did you search for it yet?

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

      @@jfversluis yep, it is "no property, BindableProperty ore event found, or mismatching type between value and property"; i'm going to investigate on the control .cs code

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

    Any advice on changes that would need to be made to make this work for data entry? I'm able to get the data to display, but getting it back out of the binding has been an issue.

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

      Hey there! Not exactly sure what you mean with this :)

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

      @@jfversluis I guess to put it simply, would this approach work for a login form as well? Where it's not just updating the label but consuming data back through an entry in the custom control to the binding context(ViewModel).

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

      Absolutely! It applies to everything :)

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

    How would you go about passing a list into a Bindable property to display in the custom control?
    I've had not much luck passing in an Ienumerable of string into the control and it's Bindable properties and displaying it on the controls CollectionView

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

      Did you figure it out yet? :)

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

      @@jfversluis Yes, looked at it with fresh eyes the next day and worked like a charm!

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

      @@nathanbrown2387 That's usually how it works! Glad it did :)

  • @solvedplus858
    @solvedplus858 10 หลายเดือนก่อน

    I created a custon control with bindableproperties,
    when i triedd to change ImageSource or WidthRequest ... etc in xaml, it never update bindableproperty value never change, i have to restart the debug to see the new values
    when i make button and set the values in it and tab on it all value update
    how to force xaml to change bindableproperty value when edit it manully in xaml
    many thanks in advance

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

    Can you create a autocompelete entry with displaymode as token in xamarin

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

      I'm sorry I'm not sure what you mean by this :)

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

    Hi again! Would you mind taking a video about the differences and use cases of a) Custom Controls b) Control Templates c) Custom Renderer ?
    Is one of these obsolete or are these just different ways towards Rome? Thanks a lot in advance!

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

      Love it! I’ll add this to the list

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

      @@jfversluis Hmm, while writing a simple CustomControl I am just wondering on how to forward the Completed Delegate? It's just a string arriving in a bound property... got stuck there ...
      normaly: Completed="{Binding Command}" now: "

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

      OK, found the answer myself, it is, if I am not misled -> Behavior. And you gave the answer already: th-cam.com/video/EKmj8Got_gE/w-d-xo.html

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

      @@TheDaple I have almost enough videos to answer anything... 😬

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

      @@TheDaple BTW thank you so much for becoming a member! Shoutout coming soon!

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

    hi Gerald, Could you tell me how to make a PDF in Xamarin? I have tried to use several libraries but it is very tedious, I do not have much time to do tests, Thanks so much friend!!! best regards

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

      You might want to have a look at this one: th-cam.com/video/y0wmCkF8iTM/w-d-xo.html
      Unfortunately, creating pdfs is hard 😅

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

    Could you please think about a Video about Error-Handling ? On the one hand it it very hard to debug code especially because of all the magic strings in Xamarin and on the other hand no sample code I have ever seen uses Error-Handling at all ! How can I prevent my App from crashing when there is e.g. just a null-string ? Is there something like a global Error-Handler ? Those kind of subject would be good and new stuff ! :-) And btw: great work so far ! I like your style !

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

      Hey there! Absolutely! What is the areas that you think are hard to debug because of these magic string? Error handling is certainly not something that can be seen in many samples :) Have a look at this session I did a while ago: th-cam.com/video/-fqPKVNOyuI/w-d-xo.html that at least handles some of the connection issues. Some of that can also be applied to other error as well.
      Thank you for the kinds words! Appreciate it :)

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

      @@jfversluis : just my latest problem: the CarouselView crashes if either the last Item is removed (now empty), the ObservableCollection is set to New ObservableCollection or the Collection is cleared like myCollection.Clear(). Big problem for me btw. I don´t expect you to solve my problem but it took me hours to figure it out, what crashes my App. No chance to debug this with my knowledge. On the other Hand I hate alle the magic string, especially in Bindings. There is no error they just don´t work and you have no clue why. I am impressed that you answered my post ! Now you are my favorite Dutch-Man (comes from a german !)

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

      ​@@LobsterHarry Haha I will pass that along to my other Dutch friends who will be jealous!
      Glad you did figure it out! And unfortunately I can't help everyone with every problem. But feel free to reach out when you got something. I can at least point you in the right direction. As long as you don't mind that it might become inspiration for a video... ;)

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

      @@jfversluis Plan B. Show us how to make an own nuget package. I do have a nice custom control to Scan Barcodes with a Bluetooth Scanner which I could share with the world...The cool thing is that this control can detect Key-Events !

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

      Very awesome! Yes! That one was already on my list, let me see if I can prioritize that one

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

    Pls, can you do a tutorial on custom control, i wanna do a custom control for entry and a picker

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

      This should tell you a lot about creating a custom control actually. I can't create the actual control for you, that's up to you ;)

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

    You saved my ass

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

      🦸‍♂️ glad I could help

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

    How about with gesture recognizer? btw, great tutorial, appreciate it!

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

      Thanks! What about it? :)

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

      @@jfversluis I was doing some custom control label with tapgesture recognizer but for some reason, ICommand is not working on custom control, I'm not sure if I'm doing the right approach, button with click event is working tho, so I'm asking if you have any tutorials or links for custom control with gesture recognizer/command

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

    Hi Sir, First of all thank you so much for the informative guide for a beginner like me. However, I have this challenge, which confuses me a little.
    Although I seem to have understood your tutorial well, but, after watching the tutorial, I came across a custom control like the one below, but, I don't know how to achieve exactly the same thing:




    What actually confuses me is how to make the child property also have the Text, Icon and Content properties.
    Please, I need your help to better understand this. Thank you once again!

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

      Thank you Derick! I'm not sure I understand what you are trying to do here...

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

    thank you for this tutorial
    I did the same thing with the same change as
    public static readonly BindableProperty EntryTextProperty =
    BindableProperty.Create("EntryText", typeof(string), typeof(LabelEntry),default,BindingMode.TwoWay);
    public string EntryText
    {
    get
    {
    return (string)GetValue(EntryTextProperty);
    }
    set
    {
    SetValue(EntryTextProperty, value);
    }
    }
    and in XAML of custom control I add
    Text="{Binding Source={RelativeSource AncestorType={x:Type local:LabelEntry}}, Path=EntryText}"
    and when I want to get the value in entry give me just the null
    any idea what is the problem
    and thank you in advance

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

      I would suspect it has to do with the RelativeSource. Have a close look at how that works, I think there is some options there you can use to make sure it finds the right reference. Else, if you could put this in a small sample project online somewhere that would really help for me to look at it :)

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

      @@jfversluis thank you for your replay and that is an example
      github.com/KevinAnass/sampleCustomControl.git

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

      @@anassnassim I've looked at it, but I'm not really clear what you are trying to do here. Sorry

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

      @@jfversluis just read the entry text hhhhhh

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

      @@anassnassim I did! And I write something, but nothing happens... No crash!

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

    lol u should said to XamMobile 🤣

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

      Wouldn't it be cool to have one?!