Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to Use Clicked event or Command Binding?

Tags:

c#

xaml

xamarin

<Button x:Name="ButtonLogin" 
        StyleId="ButtonLogin"
        Grid.Row="5" 
        BackgroundColor="#F44336" 
        BorderRadius="0" 
        TextColor="White" 
        Text="Login to Meetup"
        Command="{Binding LoginCommand}" />

or

<Button x:Name="ButtonLogin" 
        StyleId="ButtonLogin"
        Grid.Row="5" 
        BackgroundColor="#F44336" 
        BorderRadius="0" 
        TextColor="White" 
        Text="Login to Meetup"
        Clicked="LogMeIn" />

Does the use of one or the other matter specifically when designing a Xamarin app?

Which approach should be used?

like image 697
senzacionale Avatar asked Sep 15 '25 05:09

senzacionale


1 Answers

Regardless of whether it is WPF XAML, Silverlight XAML or Xamarin XAML, the choice to use one or the other does not affect the end performance of the app for at some point it all gets distilled down to codebehind and ultimately into machine code.

With that said if one uses commanding it can be designed to work within the MVVM paradigm and give direct access to functionality which can be either based on the View, ViewModel and even the Model depending on how it is set up.

Since a command is based on ICommand interface, that gives the XAML more flexibility to either allow or disallow operations due to the CanExecute functionality which is a big selling point if used.

Both can be used in Xamarin xaml template[?]

The click event is local to the control and cannot be used in a template. The click event can work with MVVM but it should not be called from anywhere outside the View that contains it.


My advice is to use Commanding where re-use particulary amongst Pages/Views is prevalent and also if there exists a need to either directly or indirectly affect styles and visibility; use it. Otherwise if the operation is just local to the page/control there is no reason not to use code-behind.

like image 71
ΩmegaMan Avatar answered Sep 16 '25 18:09

ΩmegaMan