我正在为我的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。非常感谢帮助。
答案 0 :(得分:1)
即使通过它的静态,它仍然需要在某处实例化。