Xamarin.在交换单元的代码中形成DataBinding

本文关键字:DataBinding 代码 交换 单元 Xamarin | 更新日期: 2023-09-27 18:24:03

我有以下对象:

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

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

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

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

干杯

Xamarin.在交换单元的代码中形成DataBinding

您根本没有绑定视图属性。与其将文本分配给BindingContxt和text属性,不如绑定这些text属性,即:

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

BindingContext是根据其属性进行绑定时的源对象。另请参阅DataBinding文档。