Windows Phone 7编程学习点滴一——页面切换、返回键重载和工具栏

1. 页面切换和对齐方式 2

(1)XAML实现方式

<HyperlinkButton Content="TestPage1" NavigateUri="/TestPage1.xaml" Height="30" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TestPage1Link" VerticalAlignment="Top" Width="200" />  
<HyperlinkButton Content="TestPage2" NavigateUri="/TestPage2.xaml" Height="30" HorizontalAlignment="Center" Margin="10,10,0,0" Name="TestPage2Link" VerticalAlignment="Top" Width="200" />
<HyperlinkButton Content="TestPage3" NavigateUri="/TestPage3.xaml" Height="30" HorizontalAlignment="Right" Margin="10,10,0,0" Name="TestPage3Link" VerticalAlignment="Top" Width="200" />

(2)XAML + 代码实现方式

XAML:

<Button x:Name="TestPage1Button" Content="TestPage1" Click="Button_Click" HorizontalAlignment="Left" Width="200" Height="75" />  
<Button x:Name="TestPage2Button" Content="TestPage2" Click="Button_Click" HorizontalAlignment="Center" Width="200" Height="75" />  
<Button x:Name="TestPage3Button" Content="TestPage3" Click="Button_Click" HorizontalAlignment="Right" Width="200" Height="75" />  

Code:

双点其中的按键之一,然后增加代码。

 1 private void Button_Click(object sender, RoutedEventArgs e)  
 2 {  
 3  Button clickedButton = sender as Button;  
 4  switch (clickedButton.Name)  
 5  {  
 6     case "TestPage1Button":  
 7       NavigationService.Navigate(new Uri("/TestPage1.xaml", UriKind.Relative));  
 8       break;  
 9     case "TestPage2Button":  
10       NavigationService.Navigate(new Uri("/TestPage2.xaml", UriKind.Relative));  
11       break;  
12     case "TestPage3Button":  
13        NavigationService.Navigate(new Uri("/TestPage3.xaml", UriKind.Relative));  
14        break;  
15  }  

23 返回键:Windows Phone中重写返回键的代码如下

1 protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)  
2 {  
3   // 编写的代码  
4   e.Cancel = true;  //取消默认行为。  
5 }  

3 工具栏(Expression Blend)

在MainPage.xaml文件中有段被注释掉的代码,如下:

xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"   
<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>  

一旦你在程序中添加了工具栏,你就应该能看到Visual Studio很智能地将程序的DesignHeight属性调整了72个像素。那正是工具栏的高度!

你可能会注意到它指向了两个当前不在你的项目中的图片。在 Expression Blend 中打开你的项目,导航到 “Objects and Timeline” 直到找到那些 ApplicationBarIconButton 对象。当选中任何一个 ApplicationBarIconButton 时,看一下属性标签。你会看到一种设置每个图标和文字的简便方法。

如果打开IconUri下拉框时,会看到很多可以用在程序中的标准图标。

现在,你的程序中已经有了漂亮的按钮,但在你点击它们时它不会做任何事情。此时,它们和你XAML文件中的其他东西一样。需要给它们添加一个Click事件,以及后置代码中的一个事件处理程序。下面是这个简单程序的XAML和C#代码:

XAML:

<shell:ApplicationBarIconButton x:Name="AddButton" IconUri="/icons/appbar.add.rest.png" Text="Add" Click="AddButton_Click"/>  
<shell:ApplicationBarIconButton x:Name="SubtractButton" IconUri="/icons/appbar.minus.rest.png" Text="Subtract" Click="SubtractButton_Click"/>   

代码:

1 private void AddButton_Click(object sender, EventArgs e)  
2 {  
3   Counter.Text = (Int32.Parse(Counter.Text.ToString()) + 1).ToString();  
4 }  
5 private void SubtractButton_Click(object sender, EventArgs e)  
6 {  
7   Counter.Text = (Int32.Parse(Counter.Text.ToString()) - 1).ToString();  
8 }  
原文地址:https://www.cnblogs.com/91program/p/5215681.html