Windows Phone 7后退按钮问题

时间:2010-12-11 14:15:37

标签: windows-phone-7

在我的应用中,我有几页。当我点击Windows“后退”按钮时,一切都按预期返回。

然而,我有2页让我感到悲伤。页面“A”正在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.我的代码中有什么东西导致它重新加载页面B?您也可以提供可以解释此行为的任何资源!

谢谢!

2 个答案:

答案 0 :(得分:1)

我的假设是当您按下后退按钮并再次加载页面A时会发生lbPrograms_SelectionChanged事件。

更改导航设计。对于DataTemplateStackPanel,您可以使用ManipulationStarted事件 并在里面添加

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

答案 1 :(得分:1)

由于您在OnNavigatedTo中进行的ItemsSource初始化,看起来Page A上的SelectionChanged正在触发。

您可以在SelectionChanged事件中执行任何操作之前验证SelectedIndex为-1。

或者,您可以在执行此初始化时删除SelectionChanged上的任何现有事件处理程序,并在完成时恢复该事件处理程序。