我在WP7應用程序保存在IsolatedStorage數據,這個數據是一個的ObservableCollection的ObservableCollection <T>火災SelectionChanged事件
那麼我將數據加載到應用程序一個的ObservableCollection是databinded到listview與數據模式
但是,當我這樣做(或只是將數據添加到數據綁定列表)在構造它觸發ListBox selectionchanged事件,所以在我的應用程序完全加載之前,會發生這種情況。
我對的SelectionChanged以顯示有關所點擊對象的詳細信息的事件和此崩潰發生這種情況時(的SelectedIndex是0爲某種原因使在加載的列表對象1加載時,全自動選擇)
public partial class MainPage : INotifyPropertyChanged
{
public ObservableCollection<Note> NotesCollection { get; set; }
public CollectionViewSource NotesViewSource;
private readonly IsolatedStorageSettings settings;
// Constructor
public MainPage()
{
InitializeComponent();
NotesCollection = new ObservableCollection<Note>();
settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("Notes"))
{
NotesCollection = (ObservableCollection<Note>)settings["Notes"];
}
else
{
settings.Add("Notes", NotesCollection);
}
NotesViewSource.View.Refresh();
//var note = new Note("hej", "hej", DateTime.Now, DateTime.Now);
//NotesCollection.Add(note); this also fires the event
NotesViewSource = new CollectionViewSource { Source = NotesCollection };
DataContext = this;
ListBoxNotes.ItemsSource = NotesViewSource.View;
}
我的Selectionchanged
private void ListBoxNotesSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ListBoxNotes.SelectedIndex == -1)
return;
var note = ListBoxNotes.SelectedItem as Note;
if (!(note is Note)) return;
(Application.Current as App).Note = note;
ListBoxNotes.SelectedIndex = -1;
NavigationService.Navigate(new Uri("/Views/DetailsView.xaml", UriKind.Relative));
}
問題是什麼? – 2012-01-09 20:34:28
拋出什麼異常? – 2012-01-09 21:28:02