2015-02-10 3 views
1

Я хочу сохранить значения, которые пользователь вводит в placeholder EntryCell с использованием MVVM.Xamarin Set Binding 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)); 
      } 
     } 

Когда я ввожу свои значения в поле в интерфейсе, они не спасаются здесь VM. Что я делаю не так?

Спасибо, Драгошу

+0

В вашем файле xaml вы можете попробовать изменить привязку от Placeholder to Text? –

+0

Я пробовал это, но потом я больше не могу вставлять текст в ячейку. – Dragos

+0

В вашей модели нет метода? –

ответ

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

+0

Теперь я могу вставлять значения, но они по-прежнему не сохраняются в экземпляре «newMatch». OnPropertyChanged не выполняет, у меня есть точка останова, и она не достигнута. – Dragos

+0

Является ли ваша модель просмотра реализацией INotifyPropertyChanged? У вашей страницы установлен BindingContext? –