1. 页面间数据交互我们使用CoreApplication类,它位于Windows.ApplicationModel.Core命名空间,具体为在一个页面中保存变量如下:
Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] = "Hello";
并在另一页面中读取此变量:
String str = Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] as string;
另一种方法是调用目标页面Navigate函数的第二个参数来传递参数:
Product prd = new Product()
{
ProductID = txtID.Text,
ProductName = txtName.Text
};
// 导航到目标页面并传递参数
myFrame.Navigate(typeof(PageGet), prd);
下面为软件实现流程:
2. 添加三个空页面,依次命名为Page1、Page2、Page3。
3. 在MainPage页面添加控件ListBox和Frame,代码如下:
1 <Grid.ColumnDefinitions> 2 3 <ColumnDefinition Width="Auto"/> 4 5 <ColumnDefinition/> 6 7 </Grid.ColumnDefinitions> 8 9 <ListBox Grid.Column="0" VerticalAlignment="Stretch" Width="200" 10 11 FontSize="28" 12 13 SelectionMode="Single"> 14 15 <ListBoxItem>页面一</ListBoxItem> 16 17 <ListBoxItem>页面二</ListBoxItem> 18 19 <ListBoxItem>页面三</ListBoxItem> 20 21 </ListBox>
4. 为ListBox控件添加SelectionChanged响应函数Change(),用于三个页面的切换显示:
private void Change(object sender, SelectionChangedEventArgs e) { ListBoxItem item = e.AddedItems[0] as ListBoxItem; if (item != null) { string str = item.Content as string; switch (str) { case "页面一": myFrame.Navigate(typeof(Page1)); break; case "页面二": myFrame.Navigate(typeof(Page2)); break; case "页面三":
myFrame.Navigate(typeof(Page3)); break; default: break; } } }
5. 在MainPage的初始化函数OnNavigatedTo() 中初始化显示页面一:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
myFrame.Navigate(typeof(Page1));
}
6. 为三个页面添加相应控件,代码分别如下:
页面一:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Margin="25" Text="第一个页面" Style="{StaticResource HeaderTextStyle}"/>
<StackPanel Margin="15" Grid.Row="1">
<TextBlock Text="在本页输入两个状态值,第一个状态在页面二中获取;第二个状态值在页面三中获取。" Style="{StaticResource BodyTextStyle}"/>
<TextBlock Text="输入第一个状态值:" Margin="0,13,0,0" Style="{StaticResource BodyTextStyle}" />
<TextBox Name="txt1" Margin="5,10,1066,0"/>
<TextBlock Text="输入第二个状态值:" Margin="0,18,0,0" Style="{StaticResource BodyTextStyle}"/>
<TextBox Name="txt2" Margin="5,10,1066,0"/>
<Button Margin="12,20,0,0" Width="220" Content="保存状态"/>
</StackPanel>
</Grid>
页面二:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Margin="25" Text="第二个页面" Style="{StaticResource HeaderTextStyle}"/>
<TextBlock Name="tb" FontSize="32" Margin="20,20,0,0" Grid.Row="1"/>
</Grid>
页面三:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Margin="25" Text="第三个页面" Style="{StaticResource HeaderTextStyle}"/>
<TextBlock Name="tb" Grid.Row="1" Margin="20,20,0,0" FontSize="32"/>
</Grid>
7. 为Page1的Button控件实现Click响应OnSave(),在其中保存控件中输入的数据:
private void OnSave(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt1.Text) || string.IsNullOrWhiteSpace(txt2.Text))
{
return;
}
Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] = txt1.Text;
Windows.ApplicationModel.Core.CoreApplication.Properties["value2"] = txt2.Text;
}
8. 分别在Page2和Page3(页面三中为value2)的初始化函数中读取上一步中保存的值:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (Windows.ApplicationModel.Core.CoreApplication.Properties.ContainsKey("value1")) //其中页面三中为"value2"
{
this.tb.Text = Windows.ApplicationModel.Core.CoreApplication.Properties["value1"] as string;
}
}
至此结束,运行截图如下:

