windows phone 页面传值(7)

在windows phone 中微软为我们提供了页面间传递参数的解决方案,下面就为大家介绍使用方法,页面传值的案例中我们建立两个页面,一个是MainPage另一个是SecondPage页面;MianPage页面的主要代码为:

 

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

 MainPage 设置好颜色的效果图:

 

 从上面代码可以看出我们为名为ContentPanel的Grid的属性Background设置为一个金麒麟色,当点击名为Navigation的TextBlock元素时,把ContentPanel的Grid的属性Background设置的颜色传递到第二个页面去;Navigation的事件ManipulationStarted 的隐藏代码为:

private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            //获得颜色ARGB值
            SolidColorBrush scb = (SolidColorBrush)ContentPanel.Background;
            Color c = scb.Color;
            //参数传递格式--知识点①
            Uri uri = new Uri("/SecondPage.xaml?Red="+c.R+"&Green="+c.G+"&Blue="+c.B, UriKind.Relative);
            this.NavigationService.Source = uri;
                       

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

页面需要传递的值设置好之后,会导航到第二个页面SecondPage页面,SecondPage.xaml文件中的主要代码为:

 

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

 SecondPage页面效果:

 

 从上面代码中看一看出我们并没有名为ContentPanel的Grid的属性Background设置颜色值,这里我们会从隐藏文件对其背景颜色进行设置,Navigation的事件ManipulationStarted 的隐藏代码为:

 //textblock的导航时间
        private void Navigation_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }

 

 OnNavigatedTo隐藏文件的方法是:

//活动页面构造函数之后--知识点②
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //以泛型集合的方式获得传递过来数值--知识点③
            IDictionary<string, string> para = this.NavigationContext.QueryString;
            if (para.Count>0)
            {
                byte r = Byte.Parse(para["Red"]);
                byte b = Byte.Parse(para["Blue"]);
                byte g = Byte.Parse(para["Green"]);
                ContentPanel.Background=new SolidColorBrush(Color.FromArgb(255,r,g,b));
                
            }
           
        }

此方法获得从MainPage也传递类的参数,我们进行解析,根据解析到的结果设置 SecondPage中ContentPanel.Background属性,这也就完成参数的传递了;


 

  

 

这里参数传递的格式是我们要注意的,参数是在相对URI后紧接着是问号(?),之后就是键值对的形式了(key=value),如果参数是多个,则需要用一个&符号隔开


 

这里需要用到的一个方法,即 OnNavigatedTo;官方给出的结识是: 当页面变为框架中的活动页面时调用,可以理解为从A页面导航到B页面的时候,此时B页面变为活动页面,这时候调用该方法,此方法是在活动页面构造函数加载完成之后被调用,也就是说构造函数执行完成之后就会立即执行此方法

 

NavigationContext类是获取导航的状态,唯一的一个属性 QueryString是获取查询字符串值的集合。在返回的集合类型中根据Key获得Value的值

 

记住:参数传递的格式多个参数传递需要用&符号隔开;活动页面接受参数时重写的 OnNavigatedTo方法;this.NavigationContext.QueryString接受所有传递到此页面的参数及其值,返回一个字典容器
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值