WP7中页面之间传值和保存恢复数据状态
2011-12-15 13:52
288 查看
1. MainPage.xaml,添加HyperlinkButton和TextBox控件
2. MainPage.cs,重写OnNavigatedFrom和OnNavigatedTo方法,保存MainPage中TextBox的值
3. 添加文件夹Views并添加Page1.xaml
4. Page1.xaml, 添加HyperlinkButton和TextBox控件
5. Page1.cs, 利用NavigationContext.QueryString获取id的值
<!--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); } } } }
相关文章推荐
- android -保存和恢复activity的状态数据
- Android 保存和恢复activity的状态数据
- android笔记--保存和恢复activity的状态数据
- android笔记--保存和恢复activity的状态数据
- 状态数据保存和恢复
- Android开发之保存和恢复Activity的状态与数据
- asp.net的页面状态保存与恢复
- android笔记--保存和恢复activity的状态数据
- 【转】android笔记--保存和恢复activity的状态数据
- 一天一天学 windows phone 引言 + 大概认知 之 保存恢复页面状态(八)
- Android处理运行时变更保存数据状态恢复Activity
- 保存和恢复activity的状态数据
- android笔记--保存和恢复activity的状态数据
- android保存和恢复activity的状态数据
- onSaveinstancestate 保存和恢复activity的状态数据
- Android 保存和恢复activity的状态数据
- Android保存和恢复activity状态数据
- asp.net的页面状态保存与恢复
- 【转】android笔记--保存和恢复activity的状态数据
- android笔记--保存和恢复activity的状态数据