我在 windows phone 项目的三个不同堆栈面板中设计了三个按钮,即 button1、button2、button3。我已将 button1 的可见性属性设置为可见,即 visibilty="visible" 并将其余按钮可见性属性设置为折叠,因此在运行项目时,主页应该只显示 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;
}
我想以这样的方式处理后退键按钮,当我单击按钮 1 时,按钮 2 应该显示,当我单击按钮 2 时,按钮 3 应该显示,现在如果我单击后退按钮(硬件),我应该移动到按钮 2,并且如果我再次点击后退键,我应该移动到 button1。我暂时遇到的情况是,当我在单击任何按钮后单击后退按钮时,应用程序已关闭,这是 Windows Phone 的默认行为。请帮帮我。这是我完整的 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;
}
}
}