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;

    }

そして、私はnavivedToで次のことを行います:

    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 に答える