1. MainPage.xaml,添加HyperlinkButton和TextBox控件
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="MainPage" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Height="97" Width="465" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<HyperlinkButton Content="NavigateToPage1" Height="30"
HorizontalAlignment="Left" Margin="130,83,0,0" Name="hyperlinkButton1"
VerticalAlignment="Top" Width="200"
NavigateUri="/NavigatingBetweenPages;component/Views/Page1.xaml?id=3&" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="67,152,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="346" />
</Grid>
</Grid>
2. MainPage.cs,重写OnNavigatedFrom和OnNavigatedTo方法,保存MainPage中TextBox的值
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace NavigatingBetweenPages
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
// retain the value of the textbox
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
phoneAppService.State["myValue"] = textBox1.Text;
base.OnNavigatedFrom(e);
}
// try to get the value which is retained
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
object someObject;
if (phoneAppService.State.ContainsKey("myValue"))
{
if (phoneAppService.State.TryGetValue("myValue", out someObject))
{
textBox1.Text = someObject.ToString();
}
}
base.OnNavigatedTo(e);
}
}
}
3. 添加文件夹Views并添加Page1.xaml
4. Page1.xaml, 添加HyperlinkButton和TextBox控件
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page1.xaml" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="72" HorizontalAlignment="Left" Margin="88,26,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="271" />
<HyperlinkButton Content="GoBackToMainPage" Height="30" HorizontalAlignment="Left" Margin="88,355,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="260" NavigateUri="/NavigatingBetweenPages;component/MainPage.xaml" />
</Grid>
</Grid>
5. Page1.cs, 利用NavigationContext.QueryString获取id的值
using System;
using System.Windows;
using Microsoft.Phone.Controls;
namespace NavigatingBetweenPages.Views
{
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
// get the value passed by uri(eg. /NavigatingBetweenPages;component/Views/Page1.xaml?id=3)
//this.textBlock1.Text = String.Format("Value:{0}", NavigationContext.QueryString["id"]);
string id = "";
if (NavigationContext.QueryString.TryGetValue("id",out id))
{
textBlock1.Text = String.Format("Value:{0}", id);
}
}
}
}