Xamarin.Forms 代码隐藏中的数据绑定 Button.CommandParameterProperty 到 Entry.Text

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

[最初发布在 Xamarin 论坛上 https://forums.xamarin.com/discussion/53638/data-bind-button-commandparameterproperty-to-entry-text-in-code-behind;在此处重新发布]

请翻译以下内容(来自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>

如何在代码中实现它;具体来说,如何将 loginButton.CommandParameter 数据绑定到 nameEntry.Text 以便它(文本)作为参数传递给 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; }
}

不确定您在这里寻找什么,所以这个答案有点长。 我的第一个猜测是你在 IDE 中加载了这两块代码,但什么也没发生,你想知道为什么。该示例中唯一缺少的代码块是 XAML 页面后面的代码,应该阅读。

using System;
using System.Collections.Generic;

using Xamarin.Forms;

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

您可能正在寻找的另一个可能的答案是放弃 XAML 并将整个内容翻译成代码,如果页面看起来像这样的话:

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

综上所述,MVVM 中的这些示例中的任何一个都没有实际意义。命令参数应该用于那些不属于也不应该属于您的模型的东西。我几乎从不使用它。如果您不使用 MVVM,那么它会发挥更大的作用。这样做的原因是 ViewModel 可能需要做出决策的用户名和任何其他数据元素应该绑定到 Viewmodel 中的某些内容。对于这个例子,类似于

查看:

<?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>

视图模型:

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 隐藏代码:

using System;
using System.Collections.Generic;

using Xamarin.Forms;

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

如果有帮助,代码结束于: https://github.com/DavidStrickland0/Xamarin-Forms-Samples/tree/master/LoginSample