我正在尝试编写一个非常简单的待办事项列表应用程序来试用WP8编程,但我在使用IsolatedStorage时遇到了一些麻烦。
背景:
我试图做的事情:
现在,我可以在应用运行时检索正确的数据。当用户退出应用程序并返回该应用程序时,数据全部消失。有人可以帮我找出问题所在吗?
该应用的主要代码块:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
ListItem toDoList1 = new ListItem();
public MainPage()
{
InitializeComponent();
ToDoList.ItemsSource = toDoList1.getObjList();
var settings = IsolatedStorageSettings.ApplicationSettings;
System.Diagnostics.Debug.WriteLine("Loading file... rows: {0}", settings.Count());
if (settings.Contains("myList"))
{
ListItem check;
settings.TryGetValue<ListItem>("myList", out check);
System.Diagnostics.Debug.WriteLine("Retrieved number of rows at load: {0}.", check.countRows());
}
else
{
System.Diagnostics.Debug.WriteLine("File does not exist");
}
}
private void add_Click(object sender, RoutedEventArgs e)
{
String myInput = input.Text;
toDoList1.addItem(myInput);
input.Text = "";
save();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int selected = ToDoList.SelectedIndex;
toDoList1.removeItem(selected);
save();
}
public void save()
{
var settings = IsolatedStorageSettings.ApplicationSettings;
settings.Remove("myList");
settings.Add("myList", toDoList1);
int rows = toDoList1.countRows();
System.Diagnostics.Debug.WriteLine("Number of rows at save: {0}", rows);
ListItem check;
settings.TryGetValue<ListItem>("myList", out check);
System.Diagnostics.Debug.WriteLine("Retrieved number of rows at save: {0}", check.countRows());
}
}
ListItem类:
namespace PhoneApp6
{
public class ListItem
{
private ObservableCollection<String> objList = new ObservableCollection<String>();
public ObservableCollection<String> getObjList()
{
return objList;
}
public void setObjList(ObservableCollection<String> x)
{
objList = x;
}
public void addItem(String x)
{
objList.Add(x);
}
public void removeItem(int x)
{
objList.RemoveAt(x);
}
public int countRows()
{
return objList.Count();
}
}
}
谢谢大家。