Neelam Prajapati
Neelam Prajapati

Reputation: 3802

how to update ObservableCollection<> of one page from another page in xamarin

My application is in MVVM architecture.

I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.

now i have to redirect to BarcodeScan.cs from button click of photo.xaml .

my que i how can i add item to listphoto from here(BarcodeScan.cs )??

I tried to define new list in BarcodeScan like this

  public ObservableCollection<JobPhoto> ListSerialNumbers { get; set; }

and intialised in its constructor like this

ListSerialNumbers = new ObservableCollection<JobPhoto>();

but it dont update list on photo.xaml page.

how can i achieve this. I am new to MVVM.Please Help.

Thank you.

Upvotes: 1

Views: 873

Answers (3)

maulik sakhare
maulik sakhare

Reputation: 2037

You should use messaging center for this

First get it method registered as :

MessagingCenter.Subscribe<YourObjectClassComesHere>(this, "Any Message or empty string will be okay", (Obj) =>
            {
                //Code you want to execute
            });

After this you can invoke it from another page as

MessagingCenter.Send(YourObject(of type "YourObjectClassComesHere"), "Any Message or empty string will be okay");

Hope it helps.

More details are available at : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/

Upvotes: 2

App Pack
App Pack

Reputation: 1532

I Have photo.xaml page in which i have one 1 ListView whoose bindingcontext is ObservableCollection listphoto of photos which is defined in its viewmodel.cs file.

Firstly it would be worth showing your XAML code.

You say in the quote above that you set the bindingcontext of the listview to the collection. You should be setting the ItemSource property of the ListView to the collection.

Upvotes: 0

Jesse Jiang
Jesse Jiang

Reputation: 965

You can try MessageCenter https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/

In Phonepage you subscript message and send a message from another page.

Upvotes: 1

Related Questions