Kevin Deery
Kevin Deery

Reputation: 85

Xamarin forms - MVVM observable collection always null

I have the strangest issue with an observable collection. I set my collection with some dummy data and it loads on the content page as expected however when I attempt to get the data from the data context its always null.

I debugged the code in the ViewModel and I can see the collection as null. Its clearly not null because I populates on the form.

Is there something im missing here !

private ObservableCollection<Company> _CompanyCollection;
public ObservableCollection<Company> CompanyCollection
{
   get { return _CompanyCollection; }
   set
   {
      if (value != null)
      {
        _CompanyCollection = value;
        OnPropertyChanged();
      }
   }
}

Loading data

public void LoadTestCompanies()
{
  CompanyCollection = new ObservableCollection<Company>()
  {
    new Company() { Name="The Suit Lounge"},
    new Company() { Name="The Suit Lounge"},
    new Company() { Name="The Suit Lounge"}
  };
}

Calling Viewmodel from event in page.cs

CompaniesVM viewModel = (CompaniesVM)BindingContext;
var results = viewModel.CompanyCollection.Where(x => x.Name.ToLower().Contains(searchBar.Text.ToLower()));

This is the code behind

public Companies ()
{
InitializeComponent ();
    BindingContext = new CompaniesVM(this.Navigation);           
}

ViewModel calls loatTestCompanies

   public CompaniesVM(INavigation navigation)
    {

        // Navigation 
        Navigation = navigation;

        LoadTestCompanies();

    }

Ive tried many other ways of initialising the collection and use .Add(object> but nothing seems to be working.

Any ideas would be great.

Thank you

Upvotes: 1

Views: 803

Answers (1)

Diego Rafael Souza
Diego Rafael Souza

Reputation: 5313

Two advises that may solve your problem:

1

Use a self-declared readonly property when referring to collections:

public ObservableCollection<Company> CompanyCollection { get; }

2

This change will force you to create the instance of CompanyCollection directly in the constructor:

public CompaniesVM(INavigation navigation)
{
    Navigation = navigation;
    CompanyCollection = new ObservableCollection<Company>();
    LoadTestCompanies();
}

And then...:

public void LoadTestCompanies()
{
    CompanyCollection.AddRange(new[]
        {
            new Company() { Name="The Suit Lounge"},
            new Company() { Name="The Suit Lounge"},
            new Company() { Name="The Suit Lounge"}
        });
}

I believe that changing the reference itself for bound properties implies in ViewModel using an object instance and the View using another one. So the view 'stops' to listen to VM changes for that property.

I've never got into the deep of ItemsSources Views implementations, but I guess they kind of observe the items when binding collections - or the collection instance's properties in some cases - when getting changes notification.

With this changes, I guess your code should work fine.

Hope it helps.

Upvotes: 1

Related Questions