2010-12-11 14 views
1

私のアプリではいくつかのページがあります。 Windowsの「戻る」ボタンをクリックすると、すべてが元どおりに戻ります。Windows Phone 7戻るボタンの問題

しかし、私は悲しみの原因となっている2ページを持っています。 「」いくつかは、XAMLでバインディングやっているページ:

<ListBox x:Name="lbPrograms" ItemsSource="{Binding Items}" SelectionChanged="lbPrograms_SelectionChanged"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal"> 
        <Image x:Name="ItemImage" Source="/images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/> 
        <StackPanel> 
         <TextBlock x:Name="ItemText" Text="{Binding programName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
         <TextBlock x:Name="DetailsText" Text="{Binding createDate}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/> 
        </StackPanel> 
        <Image x:Name="ItemFavs" Source="/images/favs.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/> 
        <Image x:Name="ItemDelete" Source="/images/delete.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

コードを背後ページAのためには、かなり単純ではありません:私はデータを取っていて

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    // Set the data context of the listbox control to the sample data 
    if (DataContext == null) 
     DataContext = App.ViewModel; 

    App.ViewModel.Refresh(); 
    lbPrograms.ItemsSource = App.ViewModel.Items; 
} 

private void lbPrograms_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    NavigationService.Navigate(new Uri("/DisplayProgram.xaml?selectedItem=" + lbPrograms.SelectedIndex, UriKind.Relative)); 
} 

private void BackBtn_Click(object sender, EventArgs e) 
{ 
    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
} 

ページBにはXAMLに結合していますModelViewから、それを画面上で動的に描画します。これと同じように:

private int index; 
protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    string selectedIndex = ""; 
    if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) 
    { 
     //prevents error 
     if (int.Parse(selectedIndex) == -1) 
     { 
      return; 
     } 


     if ((DataContext == null)) 
     { 
      index = int.Parse(selectedIndex); 
      App.ViewModel.Refresh(); 
      DataContext = App.ViewModel.Items[index].nValDictionary; 


      int i = 0; 
      foreach (KeyValuePair<string, string> kvp in (((System.Collections.Generic.Dictionary<string, string>)(DataContext)))) 
      { 
       StackPanel sp = new StackPanel(); 
       sp.Name = "sp" + i; 
       sp.Background = new SolidColorBrush(Colors.Black); 
       sp.Width = 460; 

       WrapPanel wp = new WrapPanel(); 
       wp.Name = "test" + i; 
       wp.Width = 300; 
       wp.Height = 200; 


       TextBlock txt = new TextBlock(); 
       txt.Text = kvp.Key.ToString(); 
       txt.Foreground = new SolidColorBrush(Colors.White); 
       sp.Children.Add(txt); 

       int chkBoxesVal = 0; 
       if (kvp.Value == "") 
       { 
        chkBoxesVal = 0; 
       }else{ 
        chkBoxesVal = Convert.ToInt32(kvp.Value.ToString()); 
       } 

       int iCount = 0; 
       for (iCount = 0; iCount <= chkBoxesVal - 1; iCount++) 
       { 
        CheckBox chk = new CheckBox(); 
        chk.Name = i.ToString(); 
        chk.Width = 56; 
        chk.Height = 70; 
        chk.Content = ""; 

        //chk.Background = new SolidColorBrush(Colors.Black); 
        //chk.BorderBrush = new SolidColorBrush(Colors.White); 

        chk.Style = (Style)Application.Current.Resources["checkBoxNG"]; 


        wp.Children.Add(chk); 
       } 

       sp.Children.Add(wp); 

       lbItems.Items.Add(sp); 
       i += 1; 
      } 
     } 
    } 
} 

}

だから私は前方つもりだとき、すべてが正常に動作しますが、私はページBにWindowsの「戻る」ボタンを押したとき、私はエラーを取得します。私は自分のコードを踏んで、 "戻る"ボタンを押すとページAに戻りますが、ページBにも行き、エラーをスローして停止します。だから誰も私にこの行動が起こっている理由を教えてもらえますか?私はそれがページAに戻り、そこで止まると期待します。私のコードにページBをリロードする何かがありますか?この行動を説明する可能性のあるリソースも歓迎します!

ありがとうございます!

答えて

1

を追加することができます。

SelectionChangedイベントでアクションを実行する前に、SelectedIndexが-1であることを確認できます。

また、この初期化を実行中にSelectionChanged上の既存のイベントハンドラを削除し、そのイベントハンドラを完了時に復元することもできます。

+0

それは基本的に私がやり遂げたこと(theSelectedIndex> 0を確認すること)です。ありがとう! @lukasのおかげで、ManipulationStartedイベントは面白いです。私はまだそれに取り組んでいます。 – webdad3

1

私が仮定しているのは、lbPrograms_SelectionChangedイベントは、戻るボタンを押してページAを再度読み込むときに発生します。

ナビゲーションデザインを変更します。あなたのDataTemplateStackPanelのために、あなたはManipulationStartedイベント を使用し、内部のあなたがOnNavigatedToでやっているのItemsSourceの初期化の結果として、発射されたページAのSelectionChangedように見えます

NavigationService.Navigate(new Uri("/DisplayProgram.xaml?selectedItem=" + lbPrograms.SelectedIndex, UriKind.Relative)); 
+0

「selectedChanged」イベントが「戻る」ボタンで発生していたのはあなたのことでした。何らかの理由で、私がManipulationStartedイベントに参加していたときにlbPrograms.SelectedIndexの値が入力されませんでした。この動作をちょっと見た後、私はManipuationStartedを取り出して、SelectedIndex == -1かどうかを調べました。それがなければ、それはそれがルーチンの残りの部分を通過することを許可した。それ以外の場合は、私はそれを停止した。それは私の問題を解決したようです。 selectedIndexの値を取得する方法に関する提案があれば教えてください。 – webdad3

関連する問題