[WP7]頁面狀態

[WP7]頁面狀態

要使用state 需要using Microsoft.Phone.Shell;

以下是MainPage.xaml.cs的code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace state
{
    public partial class MainPage : PhoneApplicationPage
    {
        PhoneApplicationService phone_state = PhoneApplicationService.Current;
        // 建構函式
        public MainPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            phone_state.State["txt1"] = textBox1.Text;   //  這裡是保存textBox1是的值
            base.OnNavigatingFrom(e);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            object objtxt;
            if (PhoneApplicationService.Current.State.ContainsKey("txt1"))
            {
                if (PhoneApplicationService.Current.State.TryGetValue("txt1", out objtxt))
                {
                    textBox1.Text = objtxt.ToString();
                }
            }
            base.OnNavigatedTo(e);
        }
        private void button1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/page1.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }

    }
}

  PhoneApplicationService phone_state = PhoneApplicationService.Current;

這行是宣告phone_state = PhoneApplicationService.Current

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    object objtxt;
    if (PhoneApplicationService.Current.State.ContainsKey("txt1"))//這行檢查state是否有"txt1"存在
    {
        if (PhoneApplicationService.Current.State.TryGetValue("txt1", out objtxt))//使用TryGetValue,如果有值就傳回True,沒值就傳回False
        {
            textBox1.Text = objtxt.ToString();//這行就是把state內的值 顯示到textBox1去
        }
    }
    base.OnNavigatedTo(e);
}

 

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
    phone_state.State["txt1"] = textBox1.Text;   //  這裡是保存textBox1是的值
    base.OnNavigatingFrom(e);
}

這段是當我們離開mainpage頁面時 會把textbox1的值存到State["txt1"]裡面

private void button1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    this.NavigationService.Navigate(new Uri("/page1.xaml", UriKind.Relative));
    e.Complete();
    e.Handled = true;
}
這段是當按下button1按鈕 換頁到page1

以下是Page1.xaml.cs的code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace state
{
    public partial class Page1 : PhoneApplicationPage
    {
        public Page1()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
         object objtxt;     
         if (PhoneApplicationService.Current.State.ContainsKey("txt1"))   
            {
                if (PhoneApplicationService.Current.State.TryGetValue("txt1", out objtxt))     
                {
                    textBox1.Text = objtxt.ToString();
                }
            }
            base.OnNavigatedTo(e);
        }
        private void button1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }
    }
}

 

下面這段

private void button1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
    this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    e.Complete();
    e.Handled = true;
}

就是回到MainPage 頁面去

兩個頁面中,主要的OnNavigatedTo 跟OnNavigatingFrom 其實 內容是一樣的,就是離開頁面就存值,進入頁面就讀值

而範例中我故意有時候用phone_state.State["txt1"] = textBox1.Text; ,有時候用PhoneApplicationService.Current.State["txt1"] = textBox1.Text;

也是要讓大家看出 phone_state = PhoneApplicationService.Current

本篇也提供完整範例下載

state.rar


如有錯誤 歡迎指正