0

我有一个 MainPage.xaml,其中是 ListBox 和 Button。当我单击该按钮时,MainPage 被导航到 AddPage.xaml。此页面用于添加新项目,有两个文本框和提交按钮。当我单击该提交按钮时,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));
        }
    }
}
4

2 回答 2

1

您需要告诉主页有要重新加载的新数据。
最简单的情况是这样的:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back)
    {
        (this.DataContext as MainViewModel).LoadData();
    }
}
于 2013-03-25T14:13:30.967 回答
0

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

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

于 2013-03-25T14:13:57.277 回答