0

在缅因州,在我的按钮单击事件处理程序上,我执行以下操作:

    private void addIconButton_Click(object sender, EventArgs e)
    {
        if (test)
        {
            MessageBox.Show("enters addIcon Main");
            Note note = new Note();
            note.Modified = DateTimeOffset.Now;

            if (note != null)
            {
               Settings.NotesList.Add(note);  //this causes the issue. 

                //Settings.NotesList[0] = note;
            }
            Settings.CurrentNoteIndex = 0;
            test = false;

            MessageBox.Show("right before navigate");
            this.NavigationService.Navigate(new Uri("/DetailsPage.XAML", UriKind.Relative));
            MessageBox.Show("after navigate");

        }
        //DetailsPage mynewPage = new DetailsPage(); 
        //this.Content = mynewPage;

    }

在我的导航到我做:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        MessageBox.Show("enters onNav Main");
        DataContext = null;
        DataContext = Settings.NotesList;


        Settings.CurrentNoteIndex = -1;
        Listbox.SelectedIndex = -1;


        if (Settings.NotesList != null)
        {
            if (Settings.NotesList.Count == 0)
            {
                Notes.Text = "No Notes";
            }
            else
            {
                Notes.Text = "";
            }
        }
    }

在我的前端代码中,我这样做:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="Listbox" SelectionChanged="listbox_SelectionChanged" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Width="800" MinHeight="60">
                        <StackPanel>
                            <TextBlock x:Name="Title" VerticalAlignment="Center" FontSize="{Binding TextSize}"  Text="{Binding Name}"/>
                            <TextBlock x:Name="Date"  VerticalAlignment="Center" FontSize="{Binding TextSize}"  Text="{Binding Modified, 
                                    Mode=TwoWay, Converter={StaticResource dateConverter}}"/>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

我在静态类中有一个可观察的集合,在构造函数中我将其设置为:

public static class Settings
{
    public static ObservableCollection<Note> NotesList;
    static IsolatedStorageSettings settings;
    private static int currentNoteIndex;

    static Settings()
    {
        NotesList = new ObservableCollection<Note>();
        settings = IsolatedStorageSettings.ApplicationSettings;
        MessageBox.Show("enters constructor settings");
    }

然后在 Notes 类中,它看起来像这样:

public class Note
{
    public DateTimeOffset Modified { get; set; }
    public string Title  { get; set; }
    public string Content { get; set; }
    public int TextSize { get; set; }

}

当我单击应用程序按钮并在调用 navigationService 后立即调用 invent 处理程序时,我得到:

    // Code to execute on Unhandled Exceptions
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            System.Diagnostics.Debugger.Break();
        }
    }

这只发生在 Settings.NotesList.Add(note); 在 addIconButton_click 方法中添加..

有什么建议么???

4

1 回答 1

0

我通过在构造函数内的 Notes 类中为实例变量设置默认值来修复它......

于 2012-11-04T04:05:44.763 回答