简体   繁体   中英

xamarin ios, MVVM light does not bind data when loading a page

I am using mvvm light for bind data in my xamarin ios app. There i am trying to change the Table header name from singular to plural according to the count of the MyCollection. But seems like when page load data does not bind.

my PageViewController

AddBinding(this.SetBinding(() => PageViewModel.Header, () => TableSource.TableHeader.LabelText));

PageViewModel.LoadData();

i have added this in ViewDidLoad().

my PageViewModel

public ObservableCollection<ViewModel> MyCollection { get; } = new ObservableCollection<ViewModel>();

public string Header =>
    (MyCollection.Count > 1 ? "Items" : "Item");

i have a method for load data to the collection it is like

public LoadData()
    {
        var data = GetData(); // for get data from service

        MyCollection.Reload(data);

        RaisePropertyChanged(Header);
    }

i called the RaisePropertyChanged() for identify the property change and to update the table header.

This is working fine after the page load.

But when the first time page load if there is some count in the MyCollection it is not binding data.

any help really appreciate.

I just did it in the below way, but i do not know it is the better way to do this.

i added default value for the Header

private string header = "Item";

public string Header
    {
        get => header;
        private set
        {
            header = value;
            RaisePropertyChanged(() => Header);
        }
    }

and set value in the LoadData Method

Header = (MyCollection.Count > 1 ? "Items" : "Item");

this is get done my work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM