你的位置:首页 > ASP.net教程

[ASP.net教程]WPF学习之路(九)导航和页面(续)


生命周期

如果Page1成功导航到Page2,首先会触发NavigationService的Navigating事件,标识导航开始。随后创建Page2对象,并且触发NavigationProgress事件。该事件用于提供导航进度信息,每次返回1KB数据就会引发该事件。随后触发Navigated事件,LoadCompleted紧随其后,这时表明页面已经下载完毕。Page1触发UnLoaded事件,宣告其结束。Page2触发Loaded事件,表明其开始。

 

 实例(取自《葵花宝典--WPF自学手册》)

App.xaml

<Application x:Class="Alex_WPFAPPDemo07.App"       ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       ="http://schemas.microsoft.com/winfx/2006/xaml"       Navigating="Application_Navigating"       NavigationFailed="Application_NavigationFailed"       Navigated="Application_Navigated"       NavigationProgress="Application_NavigationProgress"       NavigationStopped="Application_NavigationStopped"       LoadCompleted="Application_LoadCompleted"       FragmentNavigation="Application_FragmentNavigation"       StartupUri="CustomPage.xaml">  <Application.Resources>    <Style TargetType="TextBlock">      <Setter Property="FontSize" Value="15" />      <Setter Property="Margin" Value="5" />    </Style>  </Application.Resources></Application>

App.xaml.cs

public partial class App : Application{  private void Application_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_Navigating\n");    System.Console.WriteLine("导航页面的Uri:" + e.Uri.ToString());  }  private void Application_NavigationFailed(object sender, System.Windows.Navigation.NavigationFailedEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_NavigationFailed\n");    System.Console.WriteLine("失败的异常是: " + e.Exception.ToString());    // Handled属性设置为true,从而防止异常继续上传转变为一个未处理的应用程序异常     e.Handled = true;  }  private void Application_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_Navigated\n");    System.Console.WriteLine("导航页面的Uri:" + e.Uri.ToString());  }  private void Application_NavigationProgress(object sender, System.Windows.Navigation.NavigationProgressEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_NavigationProgress\n");    System.Console.WriteLine("导航页面的Uri:" + e.Uri.ToString());    System.Console.WriteLine("已经得到的字节数为{0}", e.BytesRead);  }  private void Application_NavigationStopped(object sender, System.Windows.Navigation.NavigationEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_NavigationStopped\n");    System.Console.WriteLine("导航页面的Uri:" + e.Uri.ToString());  }  private void Application_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_LoadCompleted\n");    System.Console.WriteLine("导航页面的Uri:" + e.Uri.ToString());  }  private void Application_FragmentNavigation(object sender, System.Windows.Navigation.FragmentNavigationEventArgs e)  {    System.Console.WriteLine("------------------------------------------");    System.Console.Write("触发的事件为:Application_FragmentNavigation\n");    System.Console.WriteLine("导航的段落为:" + e.Fragment);  }}

修改工程的Output type属性

 

效果如下图

 

 在控制台输出中可以看到导航的整个过程都经过了哪些事件的处理

 

 

 

 

 

 

 

 

To be continue...