刷新列表框的导航GoBack()

本文关键字:GoBack 导航 列表 刷新 | 更新日期: 2023-09-27 18:05:58

我有一个主页。xaml是列表框和按钮。当我点击按钮时,MainPage被导航到AddPage.xaml。这个页面是用来添加新条目的,有两个文本框和提交按钮。当我点击提交按钮,然后数据从文本框保存到XML文件,然后被称为GoBack()。

我需要刷新我的主页列表框。xaml当我从AddPage返回。Xaml,但它不会自动工作。我该怎么做呢?

我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));
        }
    }
}

刷新列表框的导航GoBack()

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

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

提示:您没有为视图模型的属性引发属性更改通知。

在MainPage的load事件中,调用LoadData。当你在添加任何内容之前调用LoadData方法时,你也应该清除可观察对象集合,因为简单地加载数据会导致集合中出现重复条目。