Switch Cell的代码中的Xamarin.Forms DataBinding

use*_*474 2 c# data-binding 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));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我从这样的对象获取数据://根据DB Notification notification = new Notification {中存储的内容设置通知对象

        TrafficNot = uInfo.NotificationTraffic,
        NewsNot = uInfo.NotificationNews
    };
Run Code Online (Sandbox Code Playgroud)

我想将数据绑定到这些切换台

    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
           }
        }
    };
Run Code Online (Sandbox Code Playgroud)

我还需要做些什么才能绑定它?

干杯

Mih*_*kic 8

您根本没有绑定视图属性.您应该绑定那些Text属性,而不是将文本分配给BindingContxt和Text属性,即:

var sc = new SwitchCell();
sc.SetBinding(SwitchCell.TextProperty, new Binding("NewsNot"));
Run Code Online (Sandbox Code Playgroud)

绑定其属性时,BindingContext是源对象.另请参见DataBinding文档.