您的位置:首页 > 其它

WP7中页面之间传值和保存恢复数据状态

2011-12-15 13:52 288 查看
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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: