如何仅控制主页(单页)上设计的不同页面的后退键?

时间:2013-04-30 11:32:19

标签: c# windows-phone-7

我在windows phone项目的三个不同的堆栈面板中设计了三个按钮,即button1,button2,button3。 我已将button1的visibility属性设置为visible,即visibilty =“visible”和rest按钮visibilty属性可折叠,因此在运行项目时,主页面应仅显示button1, 我还完成了所有三个不同按钮的编码,例如点击按钮1,按钮2应该显示,点击按钮2,按钮3应该显示,这里是代码..

private void button1_Click_1(object sender, RoutedEventArgs e)
{
    button1.Visibility = Visibility.Collapsed;
    stackPanel1.Visibility = Visibility.Visible;
}


    private void button2_Click(object sender, RoutedEventArgs e)
    {
        stackPanel1.Visibility = Visibility.Collapsed;
        stackPanel2.Visibility = Visibility.Visible;

    }

我想以这样的方式处理后退键按钮,当我点击button1时,按钮2 sholud显示,当我点击button2,buttton3 sholud显示,现在如果我点击后退按钮(硬件)我sholud移动到button2,再次如果我点击后退键,我应该移动到button1。 我正在经历的瞬间,当我点击任何按钮点击后面的后退按钮,应用程序关闭,这是Windows手机的defalut行为。 请帮帮我。 这是我完整的xamal代码     

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <StackPanel Height="244" HorizontalAlignment="Left" Margin="77,166,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="316">
            <Button Content="Button1" Height="71" Name="button1" Width="160" Click="button1_Click" />
        </StackPanel>

        <StackPanel Height="247" HorizontalAlignment="Left" Margin="129,240,0,0" Name="stackPanel2" VerticalAlignment="Top" Width="267" Visibility="Collapsed">
            <Button Content="Button2" Height="71" Name="button2" Width="160" Click="button2_Click" />
        </StackPanel>



        <StackPanel Height="247" HorizontalAlignment="Left" Margin="129,240,0,0" Name="stackPanel3" VerticalAlignment="Top" Width="267" Visibility="Collapsed">
            <Button Content="Button3" Height="71" Name="button3" Width="160" Click="button3_Click" />
        </StackPanel>

    </Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->

这是我的.cs代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;   
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Animation;
 using System.Windows.Shapes;
 using Microsoft.Phone.Controls;

  namespace backkeybehave
  {
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }



    private void button3_Click(object sender, RoutedEventArgs e)
    {


    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        stackPanel1.Visibility = Visibility.Collapsed;
        stackPanel2.Visibility = Visibility.Visible;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        stackPanel2.Visibility = Visibility.Collapsed;
        stackPanel3.Visibility = Visibility.Visible;
    }


   }
 }

2 个答案:

答案 0 :(得分:1)

不要这样做,后退键用于后台导航,而不是用于页面内导航。

另一方面,如果您真的想要这样做,请查看PhoneApplicationPage.OnBackKeyPress并将其用于您的应用逻辑。

答案 1 :(得分:0)

您需要实现自己的逻辑来跟踪覆盖onbackkeypress时应该发生的事情。

例如,如果在按钮3可见时按下,则应始终显示button2,您可以使用

if (button3.visibility == visibility.visible)
{
  button3.visibility = visibility.collapsed;
  button2.visibility = visibility.visible;
  e.cancel = true;
}

但是你需要仔细考虑,并包括所有可能的导航路径。