【问题标题】:WP7 - Refresh ListBox on navigation GoBack()WP7 - 在导航 GoBack() 上刷新 ListBox
【发布时间】:2013-03-25 14:07:23
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      提示:您不会针对视图模型的属性发出属性更改通知。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-16
        • 2018-03-12
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-28
        相关资源
        最近更新 更多