原文:windows phone 页面导航(6)

页面导航的例子我们使用的是两个页面,从第一个页面(MainPage)导航到第二个页面(SecondPage),然后可以从第二个页面导航到第一个页面 ,使用的os 7.1;

页面导航没有引入新的命名空间使用的到属性是派生于PhoneApplicationPage类;

MainPage.xaml 文件中用到的代码为:

 <Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0"></Grid>        <TextBlock x:Name="Navigation" Text="导航到第二个页面" Grid.Row="" VerticalAlignment="Center" HorizontalAlignment="Center" ManipulationStarted="Navigation_ManipulationStarted"></TextBlock>    </Grid>

隐藏文件代码为:

//textblock的导航时间
        private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            //为什么一定是相对的--知识点①
            this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));

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

        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            //知识点②
            SolidColorBrush scb=new SolidColorBrush ();
            //知识点③
            Color color = new Color();
            color.A = (byte);
            color.R = (byte);
            color.G = (byte);
            color.B = (byte);
            scb.Color = color;

            ContentPanel.Background = scb;
            base.OnManipulationStarted(e);
        }

隐藏文件的代码是在默认代码的基础上加上以上两个方法,实现的效果是当点击(触摸)非名为Navigation的TextBlock元素时,ContenPanel就会改变为设置好的固定颜色,MainPage效果图:

知识点①:this.NavigationService.Navigate() 是页面导航的核心方法参数是URI类型的对象,所以要New该类,并且实例化URI的时候SecondPage.xaml前有斜线,第二个参数表明    此URI是相对于SecondPage.xaml,绝对的URI是指完整引用某个资源如: http://www.sensengo.com/Index.html  相对URI取决于前面定义的基 URI(例如:/Index.html)

    URI的构造函数有一个包含了基URI和相对URI字符串:

Uri(Uri, String)

根据指定的基 URI 和相对 URI 字符串,初始化 Uri 类的新实例

     实例:

Uri baseUri = new Uri("http://www.contoso.com");      Uri myUri = new Uri(baseUri, "catalog/shownew.htm");

     所以URI实例构成了绝对URI,myUri.ToString()字符串是为:http://www.contoso.com/catalog/shownew.htm

  知识点②:SolidColorBrush类是绘制纯色区域,SolidColorBrush的另一种构造方法为:SolidColorBrush(Color),所以以上代码也可以这样实现:   
ContenPane.Backgroud= new solidColorBrush(    Color.FromArgb(,(byte)135,(byte)25,(byte)15)); 

  知识点③Color这里就是SolidColorBrush的属性color设置颜色,color类描绘的就是一个ARGB颜色,在形成图像中的每个像素的颜色表示为一个 32 位数:分别用 8 位表示 Alpha、红色、绿色和蓝色 (ARGB)。 这四个分量的值都是 0 到 255,其中 0 表示没有亮度,255 表示最大亮度。 alpha 分量指定颜色的透明度:0 表示完全透明,255 表示完全不透明。 要确定颜色的 alpha、红色、绿色或蓝色成分

SecondPage.xaml 文件主要代码:

 <Grid x:Name="ContentPanel" Grid.Row="" Margin="12,0,12,0"></Grid>        <TextBlock x:Name="Navigation" Text="导航到第-个页面" Grid.Row="" VerticalAlignment="Center" HorizontalAlignment="Center" ManipulationStarted="Navigation_ManipulationStarted"></TextBlock>    </Grid>

隐藏文件添加的两个方法代码:

 //textblock的导航时间
        private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            //两个方式实现的效果一样,但是原理不同--知识点④
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            //this.NavigationService.GoBack();
            e.Complete();
            e.Handled = true;
        }

        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            //纯色
            SolidColorBrush scb = new SolidColorBrush();
            //alpha
            Color color = new Color();
            color.A = (byte);
            color.R = (byte);
            color.G = (byte);
            color.B = (byte);
            scb.Color = color;

            ContentPanel.Background = scb;
            base.OnManipulationStarted(e);
        }

知识点④:在这里因为只有两个页面,返回到上一个页面的效果一样,但是原理不同,this.NavigationService.Navigate是实现一个新的URI实例,this.NavigationService.GoBack()则是后退到历史记录中的最新页面,如果没有历史记录页面则退出程序;

 知识点⑤ :墓碑化:因为windows phone手机不支持第三方程序后台,所以提供了另一种方法Tomstone,简单的讲就是在程序A还在运行的情况下,突然其他程序B,此时系统会暂时关闭程序A的线程,并暂存其状态;当程序B使用完毕,程序A线程会重新启动,恢复程序A之前的状态;本节的页面导航就是墓碑化的例子

小结:导航的核心一句话就是:this.NavigationService.Navigate()和返回上一个页面this.NavigationService.GoBack(),简单了解墓碑化;发现一个方  法也可以导航到其他页面如下:

  Uri uri = new Uri("/SecondPage.xaml", UriKind.Relative);            this.NavigationService.Source = uri;

最新文章

  1. OutputCache属性详解(一)一Duration、VaryByParam
  2. 《深入剖析Tomcat》读书笔记(一)
  3. 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
  4. 1483:[HNOI]2009 梦幻布丁 - BZOJ
  5. 【转】【opencv】仿射变换
  6. 自定义类StyleSheet跨浏览器操作样式表中的规则
  7. Android 图片显示
  8. Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决
  9. 《python语言程序设计》_第三章(数字函数、字符串和对象)
  10. [Linux] deepin15.8搭建LNMP环境
  11. 【Linux命令】grep命令
  12. BZOJ1390 CEOI2008 Fences 凸包、Floyd最小环/DP
  13. Springboot08-项目单元测试(接口测试)
  14. hdu1728 逃离迷宫
  15. LeetCode 90:Subsets II
  16. eclipse 的版本及下载地址
  17. 一个成功的 Git 分支模型
  18. DOM EVENT
  19. MessageFormat格式化数字
  20. 基于js alert confirm样式弹出框

热门文章

  1. Oracle 12C 简介
  2. android百度地图打包混淆 用不了No such file or directory (2) com.baidu.mapapi.BMapManager.init(Unknown Source)
  3. maven项目配置Project Facets时further configuration available不出来问题
  4. 在 Ubuntu 12.04 上通过安装源安装 Open vSwitch (OVS)
  5. [Cocos2d-x]节点的生命周期
  6. IE 兼容模式
  7. linux LVS DR模式配置
  8. File already exists: filesystem &#39;/path/file&#39;, transaction svn常见错误解决方法
  9. 盒子游戏(The Seventh Hunan Collegiate Programming Contest)
  10. SUSE Linux 报错:too many open files in system