Forum Xamarin.Forms

How to pass multiple parameters using command interface ?

ThomasFossatiThomasFossati FRMember
edited June 2015 in Xamarin.Forms

I'm new with Xamaring.Forms and I'm building an app with a login page. On the XAML code of the page i have two Entries (username and password) and a Button. I'd like to send the two parameters of the entries on my LoginViewModel class when the user clicks on the button using the command interface in order to implement correctly the MVVM design pattern.

So far I was able to make it work with one parameter using CommandParameter :

<StackLayout x:Name="entryForm" > <Entry x:Name="nameEntry" Placeholder="Name" Keyboard="Default" /> <Entry x:Name="pwdEntry" Placeholder="Password" Keyboard="Default" IsPassword="True"/> <Button x:Name="loginButton" Text="Login" Command="{Binding LoginCommand}" CommandParameter="{Binding Source={x:Reference nameEntry}, Path=Text}" /> </StackLayout>

the LoginViewModel class :

public class LoginViewModel : ViewModelBase { public LoginViewModel() { LoginCommand = new Command<string>(execute: (string parameter) => { //do something with the string username }); } public ICommand LoginCommand { get; private set; } }

I know that Multibinding is not available on Xamarin.Forms but is there any other way to do it with the command interface ? I've tried to use a converter class to build a List or a custom LoginData class and use it in my LoginCommand but without success.

Best Answer

Answers

  • ThomasFossatiThomasFossati FRMember

    Thank you for your answer, I'll do that.

  • you have used the Command in Xaml. But how to use in the xaml.cs file?

  • joaobp7joaobp7 BRMember ✭✭

    Hi @MichaelRumpler , would you have an example of how this can be done in xaml?

  • NMackayNMackay GBInsider, University admin

    @joaobp7

    If your binding in a datatemplate etc you can just do CommandParameter="{Binding .}" and the binding object will be passed to the command

  • joaobp7joaobp7 BRMember ✭✭

    Hi @NMackay, It worked perfect, thank you very much!

  • Hi @NMackay , @MichaelRumpler , @joaobp7

    New to Xamarin Forms and I'm struggling to implement this. Can anyone help me with the code.
    Xaml File, ViewModel with the ICommand and the object passed??

    Thanks.

Sign In or Register to comment.