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
總結
以上是生活随笔為你收集整理的Windows Phone 7编程学习点滴一——页面切换、返回键重载和工具栏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄山风景区离市区有多远
- 下一篇: PHP——文件操作