locked
Best way to implement an ObservableCollection whose objects are accessible from multiple views? RRS feed

  • Question

  • User390098 posted

    I am fairly new to Xamarin. I'm currently working on a project and am trying to use best MVVM practices in building it. I've successfully implemented the MVVM pattern for my login page and I have a pretty good grasp on that, but it is a little more straight forward than what I'm trying to do now.

    Once you log in you're brought to a TabbedPage. The TabbedPage contains two ContentPages, one with a ListView. My goal is to create an ObservableCollection which can be accessed by both of the ContentPages in the TabbedPage but I'm not sure where the correct place to create this ObservableCollection would be.

    Should I create a ViewModel for the TabbedPage and have the ObservableList live there and have the ViewModels for the two ContentPages access that ViewModel? Should I create the ObservableList in the ViewModel of the first ContentPage with the ListView and have the second ContentPage reference that ViewModel?

    I've tried looking for resources to understand this particular scenario but I haven't been able to find any that discuss multiple views referencing the same ObservableList. I'd be very grateful for either an explanation on the best way to do this, or a link to a resource that would explain or exemplify this. Thanks!

    I should also note that I currently have the ObservableList living in the code behind for the ContentPage with the ListView. I know this is not where it should be but I just wanted to make sure I could get the ListView working with an ObservableList and I am now looking to move it to where it should be.

    Thursday, November 7, 2019 7:56 PM

All replies

  • User382871 posted

    Create an ObservableCollection in the ViewModel class. Then set the BindingContext of the page to ViewModel and bind the ItemsSource of the ListView to the observableCollection.

    page.xaml <ListView ItemsSource="{Binding lists}"> ... </ListView> page.xaml.cs public partial class page1 : ContentPage { public page1() { InitializeComponent(); BindingContext = new _ViewModel(); } } viewmodel.cs public class _ViewModel : INotifyPropertyChanged { public ObservableCollection<model> lists { get; set; } ... }

    Friday, November 8, 2019 12:18 PM
  • User390098 posted

    This is really helpful for setting up the initial binding, but I'm looking to access the same list instance from another view as well. Does this solution allow for that? Wouldn't it have to be instantiated elsewhere?

    Friday, November 8, 2019 10:19 PM
  • User382871 posted

    I'm looking to access the same list instance from another view as well. Set the BindingContext of the page that will access the data to ViewModel.

    Monday, November 11, 2019 1:09 AM
  • User390098 posted

    I'm not really clear on what you mean. If I set the BindingContext of both pages to new _ViewModel won't this create a new instance of _ViewModel in both cases? And won't any changes to the list in one instance of _ViewModel only reflect in the view bound to that particular instance?

    Thursday, November 14, 2019 10:44 PM
  • User129559 posted

    Personally, I have usually done it where I have the TabbedPageVM, that has two children of the ChildPage1 VM and ChildPage2 VM.

    Then I bind to the appropriate ViewModel for what i am using it for.

    Thursday, November 14, 2019 11:14 PM