Ahmed.C
Ahmed.C

Reputation: 487

WP8 - ObservarableCollection, Unable to Update Listbox

So basically I'm creating this simple note app which saves and adds notes etc. The problem I'm having right now is using the ObservarableCollection to update the Listbox where all the saved notes appear. Basically I click on a button and it takes me to a page where I can write and save my note. Now when I click save. The note saves in a directory and when I return to the mainpage where the listbox is which usually loads the notes in a directory, its empty. So the only way for me to load the items back to the listbox is if I quit the app and then launch it again. That is the current problem which I have right now.

I've added the ObservableCollection. The FillListBox obviously gets called from the OnNavigatedTo event in the MainPage.

Soo far this is what I've got:

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

And in the MainPage:

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

which loads the notes into the Listbox

Upvotes: 0

Views: 168

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

Add LoadNotes = new NoteTemplateList(); before FillListBox call within OnNavigatedTo event handler.

Upvotes: 2

Related Questions