windowsphone开发页面跳转数据保存
按照一般做法,刚学会如何导航,还是不够的,因为要知道,手机里面的每个页面,就如同Web页面一样,是无状态的。
啥是无状态?如果我们玩过Web开发就明白了,当你在当前页面输入一些内容,然后退回到前一页面,再前进到该页面,就会发现,之前输入的内容可能会没了。再比如吧,你在页面A中进行了数据绑定,点击按钮后进行查询并把查询结果显示在表格中,然后你点击一个超链接,跳到D页面,然后你再从D页面退回A页面,你会发现,刚才查询的结果就不会显示了。
这就是无状态,也就是说,在你导航离开当前页面后,当前页面不会保留任何操作相关的数据。
在手机应用程序中同样如此,所以,在导航离开当前页面时保存状态信息,而在用户再次回到该页面时,恢复状态信息。
具体做法是重写两个方法:
1、OnNavigatedFrom,当导航离开当前页面后调用,在这个方法中,要把状态相关的数据保存;
2、OnNavigatedTo,当用户再次导航回该页面时,该方法被调用,这时候取出状态信息并恢复。
要读写状态信息,用到页面实例的State属性,它是一个字典,也就是键- 值对——Key - Value。
下面我们来演示一下如何保存和恢复状态信息。
新建一个WP项目,随便布局一下主页面,反正做成类似撰写邮件的页面就行了,然后放一个按钮,点击按钮后打开电话拨号程序开始打电话。
源码打印?
1.private void button1_Click(object sender, RoutedEventArgs e)
2.{
3. PhoneCallTask cc = new PhoneCallTask();
4. cc.DisplayName = "小明";
5. cc.PhoneNumber = "1342580073";
6. cc.Show();
7.}
接着重写上面说的两个方法,分别保存和读取状态。
对于State属性,不必用Add,直接用键和值设置就行了,比较我要保存姓名信息,就这样写:
this.State["Name"] = "小红";
如果字典集合中没有Name的键,会自动创建,如果有,就改写其值。对,你肯定想到了,和https://www.360docs.net/doc/c113783954.html,中我们使用Session差不多。
源码打印?
1.protected override void OnNavigatedFrom(System.Windows.Navigation.Navigation
EventArgs e)
2.{
3.this.State["content"] = ContentTextBox.Text;
4.this.State["title"] = TitleTextBox.Text;
5.base.OnNavigatedFrom(e);
6.}
7.
8.protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEv
entArgs e)
9.{
10.if (this.State.ContainsKey("title"))
11. {
12.this.TitleTextBox.Text = State["title"] as string;
13. }
14.if (this.State.ContainsKey("content"))
15. {
16.this.ContentTextBox.Text = State["content"] as string;
17. }
18.
19.base.OnNavigatedTo(e);
20.}
要注意的是,如果是读取状态信息时,记得先判断要获取数据的键是否存在,如果存在再取值,为什么?别忘了,当应用程序第一次启动时,也会调用OnNavigatedTo方法,这时候,内存中不可能保存任何状态的,所以,在取状态信息时候要记得这点。
然而,我经过实验发现,在WP 7.1的模拟器中,不需要保存状态,什么代码都不写,系统会自动保存状态,然后导航回去后,状态信息依然存在。
就是不知道真实手机上是不是这样,如果是,那WP也真是强大!
下面是完整示例代码。
[XAML]
源码打印?
1. 2.x:Class="SaveStates.MainPage" 3.xmlns="https://www.360docs.net/doc/c113783954.html,/winfx/2006/xaml/presentation" 4.xmlns:x="https://www.360docs.net/doc/c113783954.html,/winfx/2006/xaml" 5.xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.P hone" 6.xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phon e" 7.xmlns:d="https://www.360docs.net/doc/c113783954.html,/expression/blend/2008" 8.xmlns:mc="https://www.360docs.net/doc/c113783954.html,/markup-compatibility/2006" 9.mc:Ignorable="d"d:DesignWidth="480"d:DesignHeight="768" 10.FontFamily="{StaticResource PhoneFontFamilyNormal}" 11.FontSize="{StaticResource PhoneFontSizeNormal}" 12.Foreground="{StaticResource PhoneForegroundBrush}" 13.SupportedOrientations="PortraitOrLandscape"Orientation="Portrait" 14.shell:SystemTray.IsVisible="True"xmlns:my="clr-namespace:System;assembl y=mscorlib"> 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. ame="TitleTextBox"VerticalAlignment="Top"/> 42. ent="Left"Margin="13,10,0,5"Name="textBlock1"Text="正文: "VerticalAlignment="Top"Grid.Row="2"/> 43. ame="ContentTextBox"VerticalAlignment="Stretch"TextWrapping="Wrap"Vertica lScrollBarVisibility="Auto"/> 44. 45. 46. 47. 48. 49. 59. 60.
[C#]
源码打印?
https://www.360docs.net/doc/c113783954.html,ing System;
https://www.360docs.net/doc/c113783954.html,ing System.Collections.Generic;
https://www.360docs.net/doc/c113783954.html,ing System.Linq;
https://www.360docs.net/doc/c113783954.html,ing https://www.360docs.net/doc/c113783954.html,;
https://www.360docs.net/doc/c113783954.html,ing System.Windows;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Controls;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Documents;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Input;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Media;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Media.Animation;
https://www.360docs.net/doc/c113783954.html,ing System.Windows.Shapes;
https://www.360docs.net/doc/c113783954.html,ing Microsoft.Phone.Controls;
https://www.360docs.net/doc/c113783954.html,ing Microsoft.Phone.Tasks;
14.
https://www.360docs.net/doc/c113783954.html,space SaveStates
16.{
17.public partial class MainPage : PhoneApplicationPage
18. {
19.// 构造函数
20.public MainPage()
21. {
22. InitializeComponent();
23. }
24.
25.private void button1_Click(object sender, RoutedEventArgs e)
26. {
27. PhoneCallTask cc = new PhoneCallTask();
28. cc.DisplayName = "小明";
29. cc.PhoneNumber = "1342580073";
30. cc.Show();
31. }
32.
33.protected override void OnNavigatedFrom(System.Windows.Navigation.Na
vigationEventArgs e)
34. {
35.this.State["content"] = ContentTextBox.Text;
36.this.State["title"] = TitleTextBox.Text;
37.base.OnNavigatedFrom(e);
38. }
39.
40.protected override void OnNavigatedTo(System.Windows.Navigation.Navi
gationEventArgs e)
41. {
42.if (this.State.ContainsKey("title"))
43. {
44.this.TitleTextBox.Text = State["title"] as string;
45. }
46.if (this.State.ContainsKey("content"))
47. {
48.this.ContentTextBox.Text = State["content"] as string;
49. }
50.
51.base.OnNavigatedTo(e);
52. }
53. }
HTML及网页制作练习题-完整版
1、在HTML中,()标记不可出现在
和标记符之间。(C) (A)(B)
(C)