Forum Xamarin.Forms

Difference between a Command and a Function

JoshuaNovakJoshuaNovak USMember ✭✭

Can someone please explain the difference between a Command and a Function? I'm trying to setup my view model properly, but I don't know when i'd use a Command vs a regular Function.


  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    long story short, with MVVM you should use commands.
    Comand is just a fancy callback.

    using command is actually a pattern.
    the caller doesn't know (and it shouldn't) about what command is doing.
    it just executes it when needed.

    In Xamarin Forms (and Windows XAML technologies), this is implemented by System.Input.ICommand
    Some controls expose a bindable property of this type.
    As you know already, Button for example has Command property.
    What Button implementation is doing under the hood is to just call the ICommand.Execute when it's tapped.

    In your view-model you expose a Command property and you connect it to the control(Button for example) Command property.
    Button calls the ICommand instance set to its Command property, which in turn calls your callback.

    I hope this answers your question.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @AndreiNitescu I kind of get what you are saying. But what is the difference/benefit of using a Command on a Button vs using Clicked? Most examples I see using Clicked, so it's a bit confusing I guess.

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited May 2015

    Subscribing and handling control events in code-behind and implement logic there is not MVVM.
    You should use MVVM, more precisely use Xamarin Forms binding framework to "connect" your view-model to the view.
    One example of this connection is the Command pattern.

    Note "connecting". The views and view-models are decoupled and don't "know" of each other, neither of them keeps a reference to the other. The binding framework allows you to "connect" them with BindingContext property.

    Find more info here:

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    Clicked is a handler in your View whereas Command can directly be bound to a Command in your ViewModel.

    If you only want to change the UI, but nothing in the background, then using a Clicked handler is ok. If you want to navigate away, change any data, ... then you should do this in your ViewModel and thus use Command.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @MichaelRumpler So basically I use commands for data, but if I need to adjust the View I can use handlers etc. That makes sense. I assume if I need to adjust the view and load data, it would be best to call a command from a handler then?

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited May 2015

    Most controls and most of their properties can be bound to the data in view-models.
    What's really important is that the controls react (they update their UI) when data changes.

    That's because many controls have bindable properties (BindableProperty).
    So in majority of cases, you don't need to add code in the code-behind of a Page for example.

    However, reality is sometimes there are cases when you have to do some dirty coding in the Page's code-behind, updating controls.

    Another thing. You should be aware of what converters are. They are a nice way to convert data from view-model to what controls property expect.
    Search for 'converter' here to see example:

    I hope this answers your question.

  • Artur.4076Artur.4076 USUniversity ✭✭
    • Commands are always in ViewModel, Clicked event handlers are in View
    • Since Commands in ViewModel they can't access UI stuff directly, e.g. change Text of a Label, instead they change property which is bound to a Text property of Label
    • Using Commands is mandatory when you are following MVVM pattern
    • If you are following MVVM, calling a web service, query database, etc. can't be done from UI i.e. from Clicked event handler, only from ViewModel i.e. through Commands
    • Use Clicked event handlers in MVVM only when you need to do something UI specific (e.g. animation) that can't be done through ViewModel

    Use a MVVM framework (e.g. MVVMLight), it simplifies using MVVM a lot. For Xamarin I recommend you to use MVVMCross as it's cross platform

Sign In or Register to comment.