2013-03-25 76 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方法来清除可观察集合,因为只需加​​载数据将导致集合中出现重复条目​​。