简体   繁体   中英

Xamarin.Forms Data bind Button.CommandParameterProperty to Entry.Text in Code Behind

[Originally posted on Xamarin Forums https://forums.xamarin.com/discussion/53638/data-bind-button-commandparameterproperty-to-entry-text-in-code-behind ; re-posting here]

Please translate the following (from https://forums.xamarin.com/discussion/43904/how-to-pass-multiple-parameters-using-command-interface )

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

to how it should be implemented in code; specifically, how to data bind the loginButton.CommandParameter to nameEntry.Text so that it (the text) gets passed as parameter to the Command in the ViewModel:

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; }
}

Not exactly certain what your looking for here so this answer gets a bit long. My First Guess is you loaded these two chunks of code in your IDE and nothing happened and you want to know why. The only chunk of code missing from the example is the code behind of the XAML page which should read.

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace LoginFormExample
{
    public partial class LoginViewPage : ContentPage
    {
        public LoginViewPage ()
        {
            InitializeComponent ();
            BindingContext = new LoginViewModel();
        }
    }
}

The other possible answer you might be looking for is to ditch the XAML and get the whole thing translated into code if thats the case the page would look like:

using System;

using Xamarin.Forms;

namespace LoginFormExample
{
    public class LoginViewFormInCS : ContentPage
    {
        public LoginViewFormInCS ()
        {
            BindingContext = new LoginViewModel();
            Entry myEntry = new Entry ();
            Button myButton = new Button ();
            myButton.SetBinding (Button.CommandProperty, new Binding ("LoginCommand", 0)); 
            Content = new StackLayout { 
                Children = {
                    new StackLayout()
                    {
                        Children=
                        {
                            myEntry,
                            myButton
                        }
                    }
                }
            };
        }
    }
}

All that said there is no real point to either one of these examples in MVVM. The Command Parameter should be used for things that aren't and shouldn't be part of your Model. I almost never use it. If you are not using MVVM then it comes into play more. The reason for this is the Username and any other data element the View that the ViewModel might need to make decisions should be bound to something in the Viewmodel. For this example something like

View:

<?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="LoginFormExample.LoginViewPage2">
    <ContentPage.Content>
    <StackLayout x:Name="entryForm" >
  <Entry x:Name="nameEntry" Placeholder="Name" Keyboard="Default" Text="{Binding Name}"/>
  <Button x:Name="loginButton"
          Text="Login"
          Command="{Binding LoginCommand2}"
          /> 
</StackLayout>
    </ContentPage.Content>
</ContentPage>

ViewModel:

using System;
using Xamarin.Forms;
using System.Windows.Input;

namespace LoginFormExample
{
    public class LoginViewModel2
    {
        public LoginViewModel2() {

            LoginCommand2 = new Command<string>((string parameter) => 
                {
                    System.Diagnostics.Debug.WriteLine(Name);
                    //do something with the string username
                });
        }

        public string Name{ get; set; }
        public ICommand LoginCommand2 { get; private set; }
    }
}

XAML Code Behind:

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace LoginFormExample
{
    public partial class LoginViewPage : ContentPage
    {
        public LoginViewPage ()
        {
            InitializeComponent ();
            BindingContext = new LoginViewModel();
        }
    }
}

If it helps the code is over at: https://github.com/DavidStrickland0/Xamarin-Forms-Samples/tree/master/LoginSample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM