【发布时间】:2013-12-29 19:17:25
【问题描述】:
所以基本上我正在创建这个简单的笔记应用程序,它可以保存和添加笔记等。我现在遇到的问题是使用 ObservarableCollection 来更新所有已保存笔记出现的列表框。基本上,我单击一个按钮,它会将我带到一个页面,我可以在其中编写和保存我的笔记。现在当我点击保存时。笔记保存在一个目录中,当我返回列表框所在的主页时,通常将笔记加载到目录中,它是空的。因此,我将项目加载回列表框的唯一方法是退出应用程序然后再次启动它。这就是我现在遇到的问题。
我添加了 ObservableCollection。 FillListBox 显然是从 MainPage 中的 OnNavigatedTo 事件调用的。
到目前为止,这就是我所拥有的:
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));
}
}
在主页中:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
m.FillListBox();
}
将笔记加载到列表框中
【问题讨论】:
标签: c# windows-phone-8 listbox observablecollection