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.
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.
@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.
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:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_bindings_to_MVVM/
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.
@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?
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: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/
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
Xamarin Inc., as a wholly-owned Microsoft subsidiary acting as a separate legal entity, adheres to the Microsoft Privacy Statement: Privacy & cookies