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

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);
            }
        }
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值