How can I send multiple parameters in the CommandParameter ?

Hi I need send email and password in the CommandParameter and receive this parameters in the Command="{Binding LoginCommand}"


** View.xaml **

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.Views.LoginPage">

        <Entry x:Name="email" />
        <Entry x:Name="psw" IsPassword="true" />
        <Button Command="{Binding LoginCommand}" />

</ContentPage>

** View.xaml.cs **

public LoginPage()
{
    BindingContext = new LoginViewModel();
    InitializeComponent();
}

** ViewModel.cs **

public class LoginViewModel : BaseViewModel
{
    public ICommand LoginCommand { get; private set; }

    public LoginViewModel()
    {
             LoginCommand = new Command(Login);
    }

    private void Login()
    {
        MyLogin(email, password);
    }
}

Best Answer

Answers

  • DavidGuijarroDavidGuijarro USMember ✭✭
    edited March 2017

    It's OK thank you ;)

  • KayleighOliverKayleighOliver USMember ✭✭
    edited February 11

    @ClintStLaurent said:
    @DavidGuijarro

    Best choice would be to stick with an OOP outlook and create an "EmailParamsObject" or some similar class. It can have multiple properties. Then you just sent one object as your CommandPameter, and let the handler of the command break down the object into the various parts it needs.

    This helps future-proof your code. A year from now there might be 5 more parameters needed for... updated security... or whatever. You add them to the one class instead of having to scrub through a dozen places you've hard coded things.

    Besides, sending an email is more than 2 things. Lets say your app lets a user have 10 different accounts at the same time. You'll need more than two parameters. But one class can contain ALL the account information the user has chosen for a given email.

    This sounds like what I need, but I'm not sure how to pass the object through the CommandParameter. Have you got an example? The only examples I have found online are CommandParameter's that pass only one value to the method.

    EDIT:
    I actually realised I didn't need to do this to solve my problem :) However, I am interested to learn how this is done. If anyone has an example.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The only examples I have found online are CommandParameter's that pass only one value to the method.

    And will only ever be one value. That one value needs to be your custom arguments class instance. That object can then have as many things in it you like.

    Its like when you get an exception... the exception object has multiple properties... .Message, .Callstack, .InnerException etc.

    Or if you were to send just a Button as the argument. You send one thing, yet that one Button has all its normal properties like .Text, .BackgroundColor, .Width etc.

  • Mynca_2000Mynca_2000 Member ✭✭

    But how can you create your custom object in xaml?

    You're having the email and password through binding, but then you still need to create the "EmailParamsObject" in xaml. How can this be achieved?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Mynca_2000 said:
    But how can you create your custom object in xaml?

    You don't. You create a class in C#. You use that class in your viewmodels. Your XAML will send the viewmodel property it is bindinded to as the CommandParameter.

    Creating it in XAML would do you little good if your C# didn't know what it was or how to deal with it down the line. The code receiving the object would have to know what it consists of so it can use it, right? So you make it first - then use it later.

Sign In or Register to comment.