Win8页面之间传值练习

来源:互联网 发布:网络视频赌博骗局 编辑:程序博客网 时间:2024/06/10 19:24

当我们从一个界面跳到另一个界面时,可能需要从那个界面传递一些信息,那么我们怎样接受这些信息呢?

简单的不觉界面如下

 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">            <Button Name="btnPre" Style="{StaticResource PreviousAppBarButtonStyle}" AutomationProperties.Name="第一页" Click="btnPre_Click"/>            <Button Name="btnNext" Style="{StaticResource NextAppBarButtonStyle}" AutomationProperties.Name="第二页" Click="btnNext_Click"/>        </StackPanel>    </Grid>

接下来就写点击按钮的两个事件

 /// <summary>        /// 第一个按钮点击事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnPre_Click(object sender, RoutedEventArgs e)        {            //这是一个字符串变量,我们跳转的时候可以当作参数传递过去            //这是一个简单的参数,如果需要传递复杂的参数,我们就需要自定义自己的类            string parameter1 = "我是第一个页面传递来得参数";            this.Frame.Navigate(typeof(FirstPage), parameter1);        }        /// <summary>        /// 第二个按钮点击事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnNext_Click(object sender, RoutedEventArgs e)        {            string parameter2 = "我是第二个页面传递来得参数";            this.Frame.Navigate(typeof(SecondPage), parameter2);        }
一个界面简单布局

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <TextBox Name="txtFirst" Margin="200,100,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"                 MinHeight="30" MaxHeight="50" MinWidth="80" MaxWidth="200"/>    </Grid>

就是一个textBox显示传递过来的参数

怎样获取参数呢,我们是不是记得一个函数,

/// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //接受传递过来的参数
            string str =(string) e.Parameter;
            this.txtFirst.Text = str;
        }
至此,页面之间简单的参数传递搞定

原创粉丝点击