WP8 - ObservarableCollection,无法更新列表框

时间:2013-12-29 19:17:25

标签: c# windows-phone-8 listbox observablecollection

所以基本上我正在创建这个简单的笔记应用程序,它可以保存和添加笔记等。我现​​在遇到的问题是使用ObservarableCollection更新列表框,其中显示所有已保存的笔记。基本上我点击一个按钮,它将我带到一个页面,我可以写,并保存我的笔记。现在,当我点击保存。该注释保存在一个目录中,当我返回到列表框通常在目录中加载注释的主页时,它是空的。因此,我将项目加载回列表框的唯一方法是退出应用程序然后再次启动它。这就是我现在面临的当前问题。

我添加了ObservableCollection。显然,可以从MainPage中的OnNavigatedTo事件调用FillListBox。

到目前为止,这就是我所拥有的:

NoteTemplateList LoadNotes = new NoteTemplateList();

public void FillListBox()
{
    var frame = (PhoneApplicationFrame)Application.Current.RootVisual;
    var MainMenux = (MainMenu)frame.Content;

    MainMenux.lb1.ItemsSource = LoadNotes;
    MainMenux.lb2.ItemsSource = LoadNotes;
}
public class NoteTemp 
{        
    public string NoteT { get; set; }
    public string NoteB { get; set; }          
}
public class NoteTemplateList : ObservableCollection<NoteTemp>
{
    public NoteTemplateList()
    { 
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        store.CreateDirectory("/Pencil/Notes/");
        string directory = "/Pencil/Notes/";
        string[] filenames = store.GetDirectoryNames(directory);
        List<NoteTemplateList> dataSource = new List<NoteTemplateList>();
        foreach (string filename in filenames)
        {
            IsolatedStorageFileStream fileStream = store.OpenFile("/Pencil/Notes/" + filename + "/Title.txt", FileMode.Open, FileAccess.Read);
            IsolatedStorageFileStream fileStream1 = store.OpenFile("/Pencil/Notes/" + filename + "/Note.txt", FileMode.Open, FileAccess.Read);
            using (StreamReader readtitle = new StreamReader(fileStream))
            {
                var title = readtitle.ReadLine();
                using (StreamReader readbody = new StreamReader(fileStream1))
                {
                    var body = readbody.ReadLine();
                    Add(new NoteTemp { NoteT = title, NoteB = body, });
                }
            }
        }
    }
}
public class ItemProperties : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ItemProperties() { }

    private string m_NoteT;

    public string NoteT
    {
        get { return m_NoteT; }

        set
        {
            m_NoteT = value;
            OnPropertyChanged("NoteT");
        }
    }

    private string m_NoteB;

    public string NoteB
    {
        get { return m_NoteB; }
        set
        {
            m_NoteB = value;
            OnPropertyChanged1("NoteB");
        }
    }

    protected void OnPropertyChanged(string NoteT)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(NoteB));
    }

    protected void OnPropertyChanged1(string NoteB)
    {

        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(NoteB));
    }
} 

在MainPage中:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    m.FillListBox();
}

将笔记加载到列表框

1 个答案:

答案 0 :(得分:2)

LoadNotes = new NoteTemplateList();事件处理程序{/ 1}}之前添加FillListBox