[英]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.