zoukankan      html  css  js  c++  java
  • Windows Phone开发之路(15) 基本导航

      一个稍微复杂一点的应用中,可能会用到多个页面,要在这些页面中跳转,就必须用到导航功能。

      下面这个实例实现的功能是:从主页面MainPage导航到页面SecondPage,然后再从SecondPage返回到MainPage页面这样一个基本功能。

      MainPage XAML代码:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Text="Navigate to Second Page!"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Padding="0,34"
    ManipulationStarted="TextBlock_ManipulationStarted"/>
    </Grid>

      MainPage C#代码:

    public partial class MainPage : PhoneApplicationPage
    {
    // 构造函数
    public MainPage()
    {
    InitializeComponent();
    }

    private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理ManipulationStarted事件
    {
    //todo:当单击TextBlock时跳转到SecondPage页面
    this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));//调用NavigationService类的Navigate()方法实现导航

    e.Complete();//表示不再处理其它Manipulation事件
    e.Handled = true;//标记路由事件已处理,不需要向上传递事件
    }
    }

      

      SecondPage XAML代码:

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Text="Go back to Main Page"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Padding="0,34"
    ManipulationStarted="TextBlock_ManipulationStarted"/>
    </Grid>

      SecondPage C#代码:

    public partial class SecondPage : PhoneApplicationPage
    {
    public SecondPage()
    {
    InitializeComponent();
    }

    private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理ManipulationStarted事件
    {
    //todo:返回到Main Page页面
    this.NavigationService.GoBack();//调用NavigationService类的GoBack()方法返回到前一个页面,等价于下一行代码
    //this.NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative));//不同的是程序不再回到原来的MainPage实例,而是导航到一个新的MainPage实例

    e.Complete();
    e.Handled = true;
    }
    }

      效果如图:

         
     点击TextBlock导航到SecondPage页面              点击TextBlock返回到MainPage页面

      注意:1,ManipulationStarted事件,是在对UIElement对象(在这里指TextBlock)开始操作时发生。即单击它就立即发生。
         2,NavigationService属性,它是Page类中定义的只读属性,它是NavigationService类型的,而NavigationService类是密封类,我们不能对其进行实例化,而由宿主类型创建它们自己的NavigationService实例,其中NavigationService类中包含了Navigate()方法和GoBack()方法。
         3,注意this.NavigationService.GoBack()和this.NavigationService.Navigate(new Uri("/MainPage.xaml",UriKind.Relative))的区别,虽然它们实现的效果是一样的,但是它们的原理是不一样的。

      下一篇将总结Silverlight应用程序中页面间数据的传递。

  • 相关阅读:
    MyBatis入门案例
    springboot mybatis 后台框架平台 集成代码生成器 shiro 权限
    原谅这世界没那么美好
    十万的License只取决于一个连接
    Qt连接数据库的两种方法
    桃李春风一杯酒 江湖夜雨十年灯
    ERROR:Can't connect to local MySQL server through socket可能出现的情况
    MySQL忽略授权表方式<--skip-grant-tables>重置管理用户密码
    MySQL5.7和5.6初始化数据的区别
    Rsync同步过程中遇到的常见问题
  • 原文地址:https://www.cnblogs.com/mcgrady/p/2346118.html
Copyright © 2011-2022 走看看