2015-10-19 43 views
0

[原文發佈於Xamarin論壇https://forums.xamarin.com/discussion/53638/data-bind-button-commandparameterproperty-to-entry-text-in-code-behind;再在這裏發帖]Xamarin.Forms數據將Button.CommandParameterProperty綁定到代碼後面的Entry.Text

請翻譯以下(從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.CommandParameternameEntry.Text,以便它(文字)獲取視圖模型作爲參數傳遞給命令:

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

回答

0

不完全是某些你想找的這裏,所以這個答案變得有點長。 我的第一個猜測是你在你的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可能需要作出決定的View應該綁定到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

+0

感謝您的回答。我只是使用該代碼作爲示例,因爲它說明了我的問題。 我的問題更確切地說,我們可以綁定到一個Button的CommandParameterProperty,在這個例子中,這個Button在XAML中綁定了這樣的一個按鈕: 'CommandParameter =「{Binding Source = {x:Reference nameEntry},Path = Text}「' 如果僅在代碼中創建UI(因此不是XAML),如何完成此綁定?具體來說,我想將'nameEntry'中輸入的值傳遞給'LoginCommand'。 – mrtnkrstn

+0

認爲這已經解決了在Xamarin論壇如果有人在這個絆倒,並正在尋找它看到:http://forums.xamarin.com/discussion/comment/159941#Comment_159941 – David

+0

確實,它的工作,謝謝! – mrtnkrstn

相關問題