Windows Phone 7: How to notify data is updated in

2019-07-27 22:32发布

问题:

I have above scenario: If user click on ListBox, it will either have sub items (again ListBox) or detail view.

So what i did currently is: Whenever user clicks any item, made a web call and filled up the same ListBox if clicked item is having further sub items.

Now, issue comes in picture:

  1. Suppose i am in 4th screen (detail view),
  2. Moved to the 3rd and 2nd screen with data maintained as stack (Its working fine, yes i am maintaining data in ObservableCollection<ObservableCollection<MyObjects>> so while moving back, i am fetching data from there)
  3. Now, if i click any item in screen 2, it will open detail view for the screen 3 ListBox data.

Means that ListBox is not getting notified that we have filled data inside OnBackKeyPress()

FYI, i am filling up ListBox and WebBrowser in the same page., so my problem is that how do i notify ListBox once i filled up data from stack which i have maintained?

Yes i have also implemented INotifyPropertyChanged but don't know why its not working.

Please check my code:

  1. ListBox and WebView screen: http://pastebin.com/K1G27Yji
  2. RootPageItem class file with the implementation of INotifyPropertyChanged: http://pastebin.com/E0uqLtVG

sorry for pasting code in above way, i did as question is being long.

Problem:

How do i notify ListBox that data is changed from OnBackKeyPress?

回答1:

And what is the behavior if you set:

listBox1.ItemsSource = null;

before

listBox1.ItemsSource = listRootPageItems;


回答2:

This is just wrong architecture. Instead of reloading the same listbox, please add a single page for each screen. Share data between them inside the App class (internal static) and use the built in navigation stack for handling "going back". Don't override OnBackKeyPress for this purpose.

You will get your desired functionality for "free" with easier to maintain and use codebase.



回答3:

Oops it was a silly mistake i made.

I forgot to set items[] array inside OnBackKeyPress() but was accessing while clicking item, hence its having items[] data of last step we moved in forward direction, it was executing the same data.

Now, i have just included a single line and it has solved my problem.

 items = listRootPageItems.ToArray();  // resolution point

So final code of onBackKeyPress() is:

/**
 * While moving back, taking data from stack and displayed inside the same ListBox
 * */
 protected override void OnBackKeyPress(CancelEventArgs e)
 {
     listBox1.Visibility = Visibility.Visible;
     webBrowser1.Visibility = Visibility.Collapsed;
     listBox1.SelectedIndex = -1;

     if (dataStack.Count != 0)
     {
          listBox1.ItemsSource = null;
          listRootPageItems = dataStack[dataStack.Count-1];
          listBox1.ItemsSource = listRootPageItems;

               items = listRootPageItems.ToArray();  // resolution point
          dataStack.Remove(listRootPageItems);

          e.Cancel = true;
      }
 }