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 的隐藏代码为:

View Code
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 的隐藏代码为:

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

 

 OnNavigatedTo隐藏文件的方法是:

View Code
  //活动页面构造函数之后--知识点②
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //以泛型集合的方式获得传递过来数值--知识点③
            IDictionary<stringstring> 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接受所有传递到此页面的参数及其值,返回一个字典容器

 

posted @ 2012-04-12 09:04  神舟龙  阅读(2085)  评论(0编辑  收藏  举报