Hrodlfr
Hrodlfr

Reputation: 41

How to add to an ObservableCollection from a different view?

(I'm using Prism Dryloc) My application consists of two views. The first contains a single list view displaying strings and the second - an entry and a button.

The listview is bound to an observable collection in the first page's View Model. How can I add to the observable collection from a different view?

Upvotes: 2

Views: 224

Answers (1)

Saamer
Saamer

Reputation: 5099

Great question! You're essentially trying to pass data between views, and there's several ways of passing data between Views in Xamarin.Forms.

The two ways that seem relevant for your case:

  • Either making the ObservableCollection a public static object (so there's only one global instance of it). Not recommended.
  • The better way is to use the messaging center so that the second page publishes an event when the button is pressed, that the first page is subscribed to. And it passes that information that gets added to the list.

If these don't work, elaborate your use case and I'll suggest some more

Upvotes: 2

Related Questions