2015-02-10 129 views
1

我想使用MVVM將用戶輸入到EntryCell的佔位符中的值保存起來。Xamarin設置EntryCell的綁定

這是我的.xaml

<TableView> 
    <TableView.Root> 
     <TableSection> 
      <EntryCell x:Name="HomeEC" 
      Label="HomeTeam"    
      Placeholder="{Binding Home, Mode=TwoWay}" 
      >     
      </EntryCell> 

      <EntryCell x:Name="AwayEC" 
      Label="AwayTeam"    
      Placeholder="{Binding Away, Mode=TwoWay}" 
      >   
      </EntryCell> 

      <EntryCell x:Name="BetEC" 
      Label="BetTeam" 
      Placeholder="{Binding Bet, Mode=TwoWay}" 
      > 
      </EntryCell> 

      <EntryCell x:Name="TypeEC" 
      Label="BetType" 
      Placeholder="{Binding Type, Mode=TwoWay}" 
      > 
     </EntryCell> 

      <EntryCell x:Name="OddEC" 
      Label="Odd" 
      Placeholder="{Binding Odd, Mode=TwoWay}" 
      > 
      </EntryCell> 

     </TableSection>  
    </TableView.Root> 
    </TableView> 

的一部分,這是我的ViewModel類

public string Home 
     { 
      set 
      { 
       home = value; 
       OnPropertyChanged("Home"); 
       newMatch.HomeTeam = home; 
      } 
     } 


     public string Away 
     { 
      set 
      { 
       away = value; 
       OnPropertyChanged("Away"); 
       newMatch.AwayTeam = away; 
      } 
     } 

     public string Bet 
     { 
      set 
      { 
       bet = value; 
       OnPropertyChanged("Bet"); 
       newMatch.Bet = bet; 
      } 
     } 

     public string Type 
     { 
      set 
      { 
       type = value; 
       OnPropertyChanged("Type"); 
       newMatch.BetType = type; 
      } 
     } 

     public string Odd 
     { 
      set 
      { 
       odd = value; 
       OnPropertyChanged("Odd"); 
       newMatch.Odd = Decimal.Parse(odd); 
      } 
     } 



     public ICommand InsertBet; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, 
        new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

當我在UI字段中輸入自己的價值觀,他們沒有得到這裏保存VM。我究竟做錯了什麼?

感謝, 德拉戈什

+0

在您的xaml文件中,您是否可以嘗試更改從佔位符到文本的綁定? – 2015-02-10 11:23:16

+0

我試過了,但是我不能再在單元格中插入文本。 – Dragos 2015-02-10 11:30:02

+0

在您的視圖模型中沒有獲取方法? – 2015-02-10 11:33:50

回答

2

InsertMatchVM.cs

public class InsertMatchVM : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string home, away, bet; 
    public Match newMatch = new Match(); 
    public string Home 
    { 
     set 
     { 
      home = value; 
      OnPropertyChanged("Home"); 
      newMatch.HomeTeam = home; 
     } 
     get 
     { 
      return home; 
     } 
    } 


    public string Away 
    { 
     set 
     { 
      away = value; 
      OnPropertyChanged("Away"); 
      newMatch.AwayTeam = away; 
     } 
     get 
     { 
      return away; 
     } 
    } 

    public string Bet 
    { 
     set 
     { 
      bet = value; 
      OnPropertyChanged("Bet"); 
      newMatch.Bet = bet; 
     } 
     get 
     { 
      return bet; 
     } 
    }  




    public ICommand InsertBet; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, 
       new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

的Page1.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:local="clr-namespace:App2;assembly=App2" 
     x:Class="App2.Page1"> 
<ContentPage.BindingContext> 
    <local:InsertMatchVM/> 
</ContentPage.BindingContext> 

<TableView> 
    <TableView.Root> 
    <TableSection> 
    <EntryCell x:Name="HomeEC" 
     Label="HomeTeam" 
     Text="{Binding Home, Mode=TwoWay}" 
     Placeholder="Home" 
     > 
    </EntryCell> 

    <EntryCell x:Name="AwayEC" 
     Label="AwayTeam" 
     Text="{Binding Away, Mode=TwoWay}" 
       Placeholder="Away" 
     > 
    </EntryCell> 

    <EntryCell x:Name="BetEC" 
     Label="BetTeam" 
     Text="{Binding Bet, Mode=TwoWay}" 
       Placeholder="Bet" 
     > 
    </EntryCell> 
    </TableSection> 
    </TableView.Root> 
</TableView> 

</ContentPage> 

App.cs

public class App : Application 
{ 
    public App() 
    { 
     MainPage = new Page1(); 
    } 
} 

Match.cs

public class Match 
{ 
    public string HomeTeam { get; set; } 
    public string AwayTeam { get; set; } 
    public string Bet { get; set; } 
} 
+0

是的,我已閱讀該部分,它實現它。我會盡力回覆一個答案。 – Dragos 2015-02-10 12:03:45

+0

現在我可以插入值了,但他們仍然沒有保存在「newMatch」實例中。 OnPropertyChanged不執行,我只有一個斷點,它沒有達到。 – Dragos 2015-02-10 12:17:09

+0

你的viewmodel實現INotifyPropertyChanged嗎?你的頁面是否設置了BindingContext? – 2015-02-10 12:39:09