WP7 - 导航GoBack()上的刷新列表框

时间:2013-03-25 14:07:23

标签: c# windows-phone-7 xaml windows-phone-7.1

我有一个MainPage.xaml,其中是ListBox和Button。当我单击按钮时,MainPage将导航到AddPage.xaml。此页面用于添加新项目,有两个TextBoxes和提交Button。当我单击该提交Button时,TextBoxes中的数据将保存到XML文件中,然后称为GoBack()。

当我从AddPage.xaml返回时,我需要刷新我的MainPage.xaml中的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));
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要告诉主页有重新加载的新数据 最简单,就像这样:

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

答案 1 :(得分:0)

提示:您没有为View Model的属性引发属性更改通知。

在MainPage的load事件上,调用LoadData。在向其添加任何内容之前调用LoadData方法时,还应该清除observable集合,因为只需加​​载数据就会导致集合中出现重复的条目。