c# - Xamarin 设置 EntryCell 的绑定(bind)

标签 c# mvvm xamarin cross-platform

我想保存用户使用 MVVM 在 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 中。我究竟做错了什么?

谢谢,
德拉戈斯

最佳答案

插入匹配VM.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>

应用程序.cs
public class App : Application
{
    public App()
    {
        MainPage = new Page1();
    }
}

匹配.cs
public class Match
{
    public string HomeTeam { get; set; }
    public string AwayTeam { get; set; }
    public string Bet { get; set; }
}

关于c# - Xamarin 设置 EntryCell 的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28428302/

相关文章:

c# - 在 C# Regex 中检索括号之间的字符串

c# - Java 方法 getGlyphOutline 在 C# 中是否有等效项?

mvvm - 在本地化 ApplicationBar 的代码中绑定(bind) EventToCommand

c# - Xamarin 表格 : Image height request ignored inside frame inside relative layout

c# - 如何使用 Entity Framework 调用 oracle 包内的存储过程?

c# - 动态添加图像到asp.net c# gridview

c# - WPF 使用 x :Name as a key 绑定(bind)到字典

mvvm - Kendo UI Grid创建的数据未发送到 Controller

ios - F# 异步 lambda 声明

c# - Xamarin PCL库和Sqlite数据存储