According to the general practice, it is not enough to learn how to navigate, because every page in a mobile phone is stateless just like a web page.
What is stateless? If you have played web development, you will understand that when you enter some content on the current page, return it to the previous page, and then move on to the page, you will find that, the previously entered content may be missing.
For example, you have bound data in page A. click the button to query and display the query result in the Table. then you click a hyperlink to go to Page D, then you return page a from page D, and you will find that the query results will not be displayed.
This is stateless, that is, after you navigate to the current page, the current page does not retain any operation-related data.
This is also true in mobile apps. Therefore, when you navigate to the current page, save the status information and restore the status information when the user returns to the page again.
The specific method is to rewrite two methods:
1. onnavigatedfrom: it is called when the navigation bar leaves the current page. In this method, State-related data should be saved;
2. onnavigatedto: This method is called when you navigate back to the page again. At this time, the status information is retrieved and restored.
To read/write status information, use the state attribute of the page instance, which is a dictionary, that is, key-value pair -- Key-value.
The following describes how to save and restore the status information.
Create a WP project, layout the homepage at will, and create a page similar to email writing. Then, put a button and click the button to open the dialing program and start calling.
private void button1_Click (object sender, RoutedEventArgs e)
{
PhoneCallTask cc = new PhoneCallTask ();
cc.DisplayName = "Xiaoming";
cc.PhoneNumber = "1342580073";
cc.Show ();
}
Then rewrite the two methods mentioned above to save and read the state respectively.
For the State attribute, you do n’t need to use Add, just use the key and value settings. For comparison, I want to save the name information, just write:
this.State ["Name"] = "Little Red";
If there is no Name key in the dictionary collection, it will be created automatically, and if so, the value will be rewritten. Yes, you must have thought that it is similar to the use of Session in Asp.net.
protected override void OnNavigatedFrom (System.Windows.Navigation.NavigationEventArgs e)
{
this.State ["content"] = ContentTextBox.Text;
this.State ["title"] = TitleTextBox.Text;
base.OnNavigatedFrom (e);
}
protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
{
if (this.State.ContainsKey ("title"))
{
this.TitleTextBox.Text = State ["title"] as string;
}
if (this.State.ContainsKey ("content"))
{
this.ContentTextBox.Text = State ["content"] as string;
}
base.OnNavigatedTo (e);
}
It should be noted that if you are reading status information, remember to first determine whether the key to obtain data exists, and if there is a value, why? Do n’t forget that when the application starts for the first time, the OnNavigatedTo method is also called. At this time, it is impossible to save any state in the memory, so remember this when fetching state information.
However, I found through experiments that in the simulator of WP 7.1, there is no need to save the state, no code is written, the system will automatically save the state, and then navigate back, the state information still exists.
I do n’t know if this is the case on a real phone. If it is, then WP is really powerful!
Sorry, my statement above is not correct, now corrected.
To add, because the system will maintain the status of some applications, but it is limited to 5, if more than 5, the system will no longer maintain status information.
Below is the complete sample code.
[XAML]
<phone: PhoneApplicationPage
x: Class = "SaveStates.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns: phone = "clr-namespace: Microsoft.Phone.Controls; assembly = Microsoft.Phone"
xmlns: shell = "clr-namespace: Microsoft.Phone.Shell; assembly = Microsoft.Phone"
xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc: Ignorable = "d" d: DesignWidth = "480" d: DesignHeight = "768"
FontFamily = "{StaticResource PhoneFontFamilyNormal}"
FontSize = "{StaticResource PhoneFontSizeNormal}"
Foreground = "{StaticResource PhoneForegroundBrush}"
SupportedOrientations = "PortraitOrLandscape" Orientation = "Portrait"
shell: SystemTray.IsVisible = "True" xmlns: my = "clr-namespace: System; assembly = mscorlib">
<phone: PhoneApplicationPage.Resources>
<my: Double x: Key = "textSize"> 35 </ my: Double>
</phone:PhoneApplicationPage.Resources>
<!-LayoutRoot is the root grid containing all page content->
<Grid x: Name = "LayoutRoot" Background = "Transparent">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto" />
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<!-TitlePanel contains the application name 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 = "Page Name" Margin = "9, -7,0,0" Style = "{StaticResource PhoneTextTitle1Style}" />
</ StackPanel>
<!-ContentPanel-Put other content here->
<Grid x: Name = "ContentPanel" Grid.Row = "1" Margin = "12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto" />
<RowDefinition Height = "Auto" />
<RowDefinition Height = "Auto" />
<RowDefinition Height = "*" />
<RowDefinition Height = "Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row = "0" HorizontalAlignment = "Left" Margin = "13,15,0,10" Name = "textblockTitle" Text = "Title:" VerticalAlignment = "Top" FontSize = "{StaticResource textSize}" / >
<TextBox Grid.Row = "1" HorizontalAlignment = "Stretch" Margin = "2" Name = "TitleTextBox" VerticalAlignment = "Top" />
<TextBlock FontSize = "{StaticResource textSize}" HorizontalAlignment = "Left" Margin = "13,10,0,5" Name = "textBlock1" Text = "body:" VerticalAlignment = "Top" Grid.Row = "2" / >
<TextBox Grid.Row = "3" HorizontalAlignment = "Stretch" Margin = "2" Name = "ContentTextBox" VerticalAlignment = "Stretch" TextWrapping = "Wrap" VerticalScrollBarVisibility = "Auto" />
<Button Content = "Submit" Grid.Row = "4" Height = "72" HorizontalAlignment = "Stretch" Margin = "2" Name = "button1" VerticalAlignment = "Top" Click = "button1_Click" />
</ Grid>
</ Grid>
<!-Sample code demonstrating ApplicationBar usage->
<!-<phone: PhoneApplicationPage.ApplicationBar>
<shell: ApplicationBar IsVisible = "True" IsMenuEnabled = "True">
<shell: ApplicationBarIconButton IconUri = "/ Images / appbar_button1.png" Text = "Button 1" />
<shell: ApplicationBarIconButton IconUri = "/ Images / appbar_button2.png" Text = "button 2" />
<shell: ApplicationBar.MenuItems>
<shell: ApplicationBarMenuItem Text = "Menu Item 1" />
<shell: ApplicationBarMenuItem Text = "Menu Item 2" />
</shell:ApplicationBar.MenuItems>
</ shell: ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar >->
</ phone: PhoneApplicationPage>
[C #]
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.Tasks;
namespace SaveStates
{
public partial class MainPage: PhoneApplicationPage
{
// Constructor
public MainPage ()
{
InitializeComponent ();
}
private void button1_Click (object sender, RoutedEventArgs e)
{
PhoneCallTask cc = new PhoneCallTask ();
cc.DisplayName = "Xiaoming";
cc.PhoneNumber = "1342580073";
cc.Show ();
}
protected override void OnNavigatedFrom (System.Windows.Navigation.NavigationEventArgs e)
{
this.State ["content"] = ContentTextBox.Text;
this.State ["title"] = TitleTextBox.Text;
base.OnNavigatedFrom (e);
}
protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
{
if (this.State.ContainsKey ("title"))
{
this.TitleTextBox.Text = State ["title"] as string;
}
if (this.State.ContainsKey ("content"))
{
this.ContentTextBox.Text = State ["content"] as string;
}
base.OnNavigatedTo (e);
}
}
}