2013-03-25 74 views
0

我有一個MainPage.xaml,其中是ListBox和Button。當我點擊按鈕時,MainPage將導航到AddPage.xaml。此頁面用於添加新項目,有兩個文本框並提交按鈕。當我點擊提交按鈕時,然後將來自TextBoxes的數據保存到XML文件中,然後稱爲GoBack()。WP7 - 在導航上刷新ListBox GoBack()

當我從AddPage.xaml返回時,需要刷新ListPage中的ListBox,但它不能自動工作。我怎樣才能做到這一點?

我MainViewModel.cs

public class MainViewModel : INotifyPropertyChanged 
{ 
    public ObservableCollection<Context> Contexts { get; private set; } 

    public MainViewModel() 
    { 
     this.Contexts = new ObservableCollection<Context>(); 
    } 

    public bool IsDataLoaded 
    { 
     get; 
     private set; 
    } 

    public void LoadData() 
    { 
     try 
     { 
      var file = IsolatedStorageFile.GetUserStoreForApplication(); 
      XElement xElem; 

      using (IsolatedStorageFileStream read = file.OpenFile("contexts.xml", FileMode.Open)) 
      { 
       xElem = XElement.Load(read); 
      } 

      var contexts = from context in xElem.Elements("Context") 
          orderby (string)context.Element("Name") 
          select context; 

      foreach (XElement xElemItem in contexts) 
      { 
       Contexts.Add(new Context 
       { 
        Name = xElemItem.Element("Name").Value.ToString(), 
        Note = xElemItem.Element("Note").Value.ToString(), 
        Created = xElemItem.Element("Created").Value.ToString() 
       }); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

     this.IsDataLoaded = true; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

和Context.cs

public class Context : INotifyPropertyChanged 
{ 
    private string _name; 
    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
     set 
     { 
      if (value != _name) 
      { 
       _name = value; 
       NotifyPropertyChanged("Name"); 
      } 
     } 
    } 

    private string _note; 
    public string Note 
    { 
     get 
     { 
      return _note; 
     } 
     set 
     { 
      if (value != _note) 
      { 
       _note = value; 
       NotifyPropertyChanged("Note"); 
      } 
     } 
    } 

    private string _created; 
    public string Created 
    { 
     get 
     { 
      return _created; 
     } 
     set 
     { 
      if (value != _created) 
      { 
       _created = value; 
       NotifyPropertyChanged("Created"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

回答

1

你需要告訴主網頁,有重新加載新的數據。
在它的最簡單的,是這樣的:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    if (e.NavigationMode == NavigationMode.Back) 
    { 
     (this.DataContext as MainViewModel).LoadData(); 
    } 
} 
0

提示:你是不是養了財產的視圖模型的屬性改變的通知。

在MainPage的加載事件上調用LoadData。在添加任何內容之前,您應該先調用LoadData方法來清除可觀察集合,因爲只需加​​載數據將導致集合中出現重複條目​​。