windows phone8.1:頁面導航詳解


小夢給大家帶來windows phone 8.1應用開發實戰教程,分享自己學習,開發過程中的經驗和技巧。

今天給大家分享windows phone 8.1頁面導航相關知識。涉及知識點如下:

  1. 頁面一導航到頁面二
  2. 頁面一帶一個參數導航到頁面二
  3. 頁面一帶多個參數導航到頁面二
  4. 重寫后退鍵,使得后退鍵可以返回前一頁面。注:后退鍵默認是退出程序。
  5. 清楚后退堆棧歷史。

本例共有三個頁面:MainPage,BlankPage1,BlankPage2。

MainPage,BlankPage1的前台代碼如下:

 
         

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="這是主頁" Grid.Row="0" FontSize="40"></TextBlock>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="網站:" FontSize="40" Margin="0,0,0,30" Width="88"/>
<TextBox x:Name="textName" Height="40" Margin="0,10,0,0" Width="260" />
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<TextBlock Text="網址:" FontSize="40" Margin="0,0,0,30" Width="88"/>
<TextBox x:Name="textAdress" Height="40" Margin="0,10,0,0" Width="260" />
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<TextBlock Text="內容:" FontSize="40" Margin="0,0,0,30" Width="88"/>
<TextBox x:Name="textContent" Height="40" Margin="0,10,0,0" Width="260" />
</StackPanel>
<Button Content="去第二頁" Grid.Row="4" Margin="122,147.667,0,147" Click="Button_Click"/>

 
         

</Grid>

 

 

BlankPage2的前台代碼如下:

 1 <Grid>
 2 <Grid.RowDefinitions>
 3 <RowDefinition Height="Auto"/>
 4 <RowDefinition Height="80"/>
 5 <RowDefinition Height="*"/>
 6 </Grid.RowDefinitions>
 7 <TextBlock Text="這是第三頁" Grid.Row="0" FontSize="40"></TextBlock>
 8 <TextBox Name="text" Grid.Row="1" Height="60" Margin="0,9.667,10,0"/>
 9 <Button Content="我要直接回主頁" Grid.Row="2" Margin="122,147.667,0,147" Click="Button_Click"/>
10 </Grid>

 

mainpage的后台關鍵代碼如下:

 1 private void Button_Click(object sender, RoutedEventArgs e)
 2 
 3 {
 4 Website web=new Website()
 5 {
 6 Name=this.textName.Text,
 7 Address=this.textAdress.Text,
 8 Content=this.textContent.Text
 9 };
10 Frame.Navigate(typeof(BlankPage1),web); //對應知識點3,即傳遞多個參數時建立一個對象,傳遞對象即可。
11 }

blankpage1的后台關鍵代碼如下:

 1 protected override void OnNavigatedTo(NavigationEventArgs e)//重寫OnNavigatedTo接受從maingape頁面傳遞來的參數。
 2 {
 3 var web = (Website)e.Parameter;
 4 this.textName.Text = web.Name;
 5 this.textAdress.Text = web.Address;
 6 this.textContent.Text = web.Content;
 7 HardwareButtons.BackPressed += HardwareButtons_BackPressed;//注冊重寫后退按鈕事件
 8 }
 9 
10 private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)//重寫后退按鈕,如果要對所有頁面使用,可以放在App.Xaml.cs的APP初始化函數中重寫。
11 {
12 e.Handled = true;
13 
14 if (Frame.CanGoBack)
15 Frame.GoBack();
16 }
17 
18 private void Button_Click(object sender, RoutedEventArgs e)//傳遞單個參數
19 
20 {
21 Frame.Navigate(typeof(BlankPage2), "我是從第二頁傳送過來的!!");
22 }

 

blankpage2的關鍵后台代碼:

 1 protected override void OnNavigatedTo(NavigationEventArgs e)
 2 {
 3 text.Text = (string)e.Parameter;
 4 Frame.BackStack.RemoveAt(Frame.BackStackDepth - 1);//對應知識點5,清楚后退堆棧的最后一條歷史,本例中即第二頁,刪除后即可從第三頁直接到主頁。
 5 }
 6 
 7 private void Button_Click(object sender, RoutedEventArgs e)
 8 {
 9 Frame.GoBack();
10 }

 

運行效果:

windows Phone8.1開發頁面導航 windows phone 8.1開發教程 windows phone8.1應用開發

源代碼下載:

頁面導航源代碼

PS:希望熱愛windows phone開發的大神,小白能來編程小夢一起多多交流,windows Phone開發者本身就比較少,小夢希望大家有一個交流的平台,歡迎大家光臨!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM