我有以下对象:

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
           }
        }
    };


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

干杯

最佳答案

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

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


绑定上下文是您绑定其属性时的源对象。另请参见DataBinding docs

关于c# - Xamarin.Forms DataBinding中的代码用于开关单元,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25985857/

10-09 16:03