C# 开关单元代码中的表单数据绑定

C# 开关单元代码中的表单数据绑定,c#,data-binding,xamarin.forms,C#,Data Binding,Xamarin.forms,我的目标如下: public class Notification : INotifyPropertyChanged { private bool _trafficNot; public bool TrafficNot { get { return _trafficNot; } set { if (value.Equals(_trafficNot)) return;

我的目标如下:

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
    };
我想把数据绑定到这些交换机上

    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
           }
        }
    };
我还需要做什么来绑定它


干杯

您根本没有绑定视图属性。您应该绑定这些文本属性,而不是将文本指定给BindingContext和text属性,即:

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

BindingContext是您根据其属性进行绑定时的源对象。另请参见。

谢谢-它不允许我在TableSection中命名SwitchCell,因此我认为它们无法命名-但感谢您的回答,我在表外进行了声明。