我正在为我的 Windows Phone 课程开发一个程序,但遇到了一些问题。当我尝试启动应用程序时,在尝试访问静态 ObservableCollection 时出现空引用异常。我认为因为它是静态的,所以我不需要实例化它。我在这里做错了吗?这是方法:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
this.DataContext = null;
this.DataContext = Settings.NotesList;
Settings.CurrentNoteIndex = -1;
TheListBox.SelectedIndex = -1;
if (Settings.NotesList.Count <= 0) // EXCEPTION
{
NoteStatus.Visibility = System.Windows.Visibility.Visible;
TheListBox.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
NoteStatus.Visibility = System.Windows.Visibility.Collapsed;
TheListBox.Visibility = System.Windows.Visibility.Visible;
}
}
在一个单独的文件中我有:
public static class Settings
{
static Settings() { }
public static ObservableCollection<Note> NotesList;
static IsolatedStorageSettings settings;
private static int currentNoteIndex;
public static int CurrentNoteIndex { get; set; }
}
我想在再写之前测试程序,但我不确定是什么原因造成的。OnNavigatedTo 来自启动应用程序,因此我什至从未访问过 MainPage.xaml。非常感谢帮助。