2014-09-23 19 views
2

我有以下目的:Xamarin.Forms数据绑定在代码为开关单元

public class Notification : INotifyPropertyChanged 
{ 
    private bool _trafficNot; 
    public bool TrafficNot 
    { 
     get { return _trafficNot; } 
     set { 
       if (value.Equals(_trafficNot)) 
        return; 
       _trafficNot = value; 
       OnPropertyChanged(); 

      } 
    } 

    private bool _newsNot; 
    public bool NewsNot 
    { 
     get { return _newsNot; } 
     set 
     { 
      if (value.Equals(_newsNot)) 
       return; 
      _newsNot = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    void OnPropertyChanged([CallerMemberName]String propertyName=null) 
    { 
     var handler=PropertyChanged; 
     if(handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

} 

我从一个这样的对象的数据:根据什么被存储在 //设置通知对象数据库 通知通知=新通知 {

 TrafficNot = uInfo.NotificationTraffic, 
     NewsNot = uInfo.NotificationNews 
    }; 

,我希望将数据绑定到这些switchells

TableView tableView = new TableView 
    { 
     BindingContext = notification, 
     Intent = TableIntent.Form, 
     Root = new TableRoot 
     { 
      new TableSection 
      { 
       new SwitchCell 
       { 
        Text = "News", 
        BindingContext = "NewsNot" 

       }, 
       new SwitchCell 
       { 
        Text = "Traffic", 
        BindingContext = "TrafficNot" 
       }, 
       new SwitchCell 
      } 
     } 
    }; 

我还需要做什么来绑定它?

干杯

回答

7

您根本没有绑定视图属性。相反,以BindingContxt和Text属性指定的文本,你应该绑定的Text属性,即:

var sc = new SwitchCell(); 
sc.SetBinding(SwitchCell.TextProperty, new Binding("NewsNot")); 

的BindingContext是源对象,同时要针对其属性绑定。另见DataBinding docs

+0

啊,谢谢你纠正这个错字,史蒂芬。 – 2014-09-24 07:19:55

+0

谢谢 - 它不让我在TableSection中命名SwitchCell,所以我认为它们不能被命名 - 但是由于你的回答,我努力在桌子外面宣布。 – user1667474 2014-09-24 08:46:59