2017-06-01 52 views
0

我的應用程序是在MVVM架構中。如何從xamarin的另一頁更新一頁的ObservableCollection <>

我有photo.xaml頁面,其中我有一個1 ListView whoose bindingcontext是ObservableCollection在其viewmodel.cs文件中定義的照片listphoto。

現在我必須重定向到BarcodeScan.cs從按鈕點擊photo.xaml。

我的問題我如何添加項目listphoto從這裏(BarcodeScan.cs)?

我試圖在BarcodeScan這樣定義

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

新的列表,並在其構造intialised這樣

ListSerialNumbers = new ObservableCollection<JobPhoto>(); 

但photo.xaml頁上不更新列表。

我該如何做到這一點。我是MVVM的新手。請幫助。

謝謝。

回答

1

您應該使用通訊中心,這

先得到它的方法註冊爲:

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

在此之後,你可以從另一個頁面調用它作爲

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

希望它能幫助。

更多詳細信息,請訪問:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/

+0

非常感謝你:) –

0

我有photo.xaml頁面,其中我有一個1 ListView whoose bindingcontext是ObservableCollection在其viewmodel.cs文件中定義的照片listphoto。

首先它值得顯示你的XAML代碼。

您在上面的引用中說過,您將listview的bindingcontext設置爲集合。您應該將ListView的ItemSource屬性設置爲集合。

相關問題