Win 8 中,页面参数递参数是通过调用Frame类的Navigate方法,它有两个重载,其中一个是可以传参数的。
1、界面设计如下:
<Grid Background="LightCoral"> <TextBlock HorizontalAlignment="Left" Margin="277,51,0,0" TextWrapping="Wrap" Text="学号:" FontSize="30" VerticalAlignment="Top"/> <TextBlock HorizontalAlignment="Left" Margin="277,106,0,0" TextWrapping="Wrap" Text="姓名:" FontSize="30" VerticalAlignment="Top"/> <TextBox x:Name="StuNum" HorizontalAlignment="Left" Margin="386,51,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="148"/> <TextBox x:Name="StuName" HorizontalAlignment="Left" Margin="386,106,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="148"/> <Button x:Name="OK" Content="确定" HorizontalAlignment="Left" Margin="277,172,0,0" VerticalAlignment="Top" Click="OK_Click"/> </Grid>
2、设计了一个Student类,方便传递参数
public class Student { public string stuNum { get; set; } public string StuName { get; set; } }
3、关键代码如下: 给参数赋值,然后通过Frame.Navigate方法,传递参数,并导航到GetParameters.xaml页面中。
private void OK_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(StuName.Text) == true || string.IsNullOrEmpty(StuNum.Text) == true) return; Student stu = new Student() { stuNum = StuNum.Text, StuName = StuName.Text }; Frame CurFrame = Window.Current.Content as Frame; if (CurFrame != null) { CurFrame.Navigate(typeof(GetParameters), stu); } }
4、获得参数。在GetParameters.xaml页面中显示参数。
protected override void OnNavigatedTo(NavigationEventArgs e) { Student stu = e.Parameter as Student; if (stu != null) { ShowMsgTbk.Text = " [获得参数页] -> " + "学号: " + stu.stuNum + " 姓名: " + stu.StuName; } }
5、效果图
图1
图2.
总结:Win8 中页面传参可以通过Frame.Navigate方法来实现。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!