简体   繁体   English

Xamarin.Forms数据将Button.CommandParameterProperty绑定到背后代码中的Entry.Text

[英]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 ; [最初发布在Xamarin论坛上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 ) 请翻译以下内容(来自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: 具体来说,如何将loginButton.CommandParameter绑定到nameEntry.Text以便如何将其(文本)作为参数传递给ViewModel中的Command:

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. 我的第一个猜想是,您在IDE中加载了这两个代码块,但没有任何反应,您想知道为什么。 The only chunk of code missing from the example is the code behind of the XAML page which should read. 该示例中缺少的唯一代码块是应阅读的XAML页面后面的代码。

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: 您可能正在寻找的另一种可能的答案是,放弃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
                        }
                    }
                }
            };
        }
    }
}

All that said there is no real point to either one of these examples in MVVM. 综上所述,MVVM中的任何一个示例都没有真正意义。 The Command Parameter should be used for things that aren't and shouldn't be part of your Model. Command Parameter应该用于不属于模型的部分。 I almost never use it. 我几乎从不使用它。 If you are not using MVVM then it comes into play more. 如果您不使用MVVM,它将发挥更大的作用。 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. 原因是用户名和ViewModel可能需要做出决定的View的任何其他数据元素都应绑定到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: 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: 背后的XAML代码:

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 如果有帮助,则代码在以下位置结束: https : //github.com/DavidStrickland0/Xamarin-Forms-Samples/tree/master/LoginSample

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms MVVM 在 Entry 和 Property 之间的双向绑定不起作用(Entry.Text 未使用 PropertyChanged 更新) - Xamarin.Forms MVVM two way binding between Entry and Property not working(Entry.Text not updated with PropertyChanged) Xamarin:从后面的代码中的事件处理程序获取 Entry.Text 的绑定属性 - Xamarin: Get bound property of Entry.Text from event handler in code behind Xamarin.Forms:绑定到 XAML 中的代码隐藏属性 - Xamarin.Forms: bind to a code behind property in XAML 将 XAML “Label”绑定到 Xamarin.Forms 中“public const”后面的代码? - Bind XAML “Label” to Code Behind “public const” in Xamarin.Forms? 将条目文本绑定到Xamarin.Forms中的按钮命令的CanExecute方法 - Bind Entry TextChanged to the CanExecute method of button command in Xamarin.Forms 如何将Xamarin.Forms条目绑定到非字符串类型,例如Decimal - How to bind Xamarin.Forms Entry to a Non String Type such as Decimal 如何使用代码隐藏在 Xamarin.Forms 中创建单击事件处理程序的按钮? - How to create a button clicked EventHandler in Xamarin.Forms with code-behind? 如何在 MVVM 之后将数据从 ViewModel 传递到 Xamarin.Forms 后面的代码? - How to pass data from ViewModel to View code behind in Xamarin.Forms following MVVM? 在后面的代码中修改使用 XAML 创建的网格 - Xamarin.Forms - Modify Grid created using XAML in code behind - Xamarin.Forms 在后面的代码中设置 Xamarin.Forms 绑定 CommandParameter - Set Xamarin.Forms binding CommandParameter in code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM