Reputation: 67
I have an application that has two pages: MainPage.xaml
and ShowResultsPage.xaml
.
My program will start an asynchronous request call in a third separate class when a button is clicked on the MainPage
.
From there, I receive a JSON
response and deserialize
it asynchronously in a non-UI based thread (in the third class).
After the call is completed, I navigate to the ShowResultsPage.xaml
where a ListBox
is located. What I need to do is update this ListBox
with the response I received in the asynchronous request.
I try to do this using the CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
method to call another method from the ShowResultsPage.xaml.cs
that would access the response from the asynchronous call and update the ListBox
.
When this method runs it doesn't actually populate the ListBox
on the ShowResultsPage
.
Here is the code inside of the third class that runs the non-UI based thread:
private ShowResultsPage resultsPage = new ShowResultsPage();
private void GetResponseCallBack(IAsyncResult ar)
{
// Gets Response and successfully deserializes it into a List of information called "details"
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, delegate()
{
this.frame.Navigate(typeof(ShowResultsPage));
resultsPage.AddToListBox(details);
});
}
Next, it goes to the ShowResultsPage.xaml.cs and runs this method from there:
public void AddToListBox(List<Station> details)
{
foreach (Station s in details)
{
this.listBoxStations.Items.Add(s.station.ToString());
}
}
I placed a break point at the start of the AddToListBox method to make sure that it's being called, and it is, but the ListBox still remains empty.
Thank you for your time in reading this!
Upvotes: 0
Views: 387
Reputation: 15006
You might want to do something like this. First, send the details List as part of the navigation:
this.frame.Navigate(typeof(ShowResultsPage), details);
Then in ShowResultsPage, in OnNavigatedTo override, you can get the navigation parameter in NavigationEventArgs parameter which contains a Parameter object which you can convert to List and call AddToListBox (or, if the ListBox is not yet created when OnNavigatedTo is called, save the List locally during OnNavigatedTo and then call AddToListBox in Page.Loaded event handler)
Upvotes: 1