wp7-页面值传递及小生命周期

xaml页跟xaml页之间的值传递:

Page1页面中传递值:

 

1  NavigationService.Navigate(new Uri("/Page1.xaml?name=" + txtName.Text, UriKind.Relative));

 

 

 

在page1中接受传递过来的值

 

1 textBlock1.Text=  NavigationContext.QueryString["name"];

 

当我们传递比较复杂的值的时候 就不能像上面那样简单的传递,比如a=b&c=d,我们就要考虑用EscapeDataString来进行转义后在传递

1    NavigationService.Navigate(new Uri("/Page1.xaml?name="+Uri.EscapeDataString("a=b&c=d") , UriKind.Relative));

当我们需要传递一个比上面还要复杂的对象怎么办呢?比如我们需要传递的是一个Buttun按钮

 

 1  private void button1_Click(object sender, RoutedEventArgs e)
 2         {
 3             NavigationService.Navigate(new Uri("/Page1.xaml?name="+Uri.EscapeDataString("a=b&c=d") , UriKind.Relative));
 4             Page1.btn = button1;
 5         }
 6 
 7 
 8   public static Button btn;
 9         private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
10         {
11             textBlock1.Text = (string)btn.Content;
12         }

 

 

 

这样就把前一个页面的Button按钮对象传递过来了 !非常的灵活

 

 1 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
 2         {
 3             MessageBox.Show("PhoneApplicationPage_Loaded");
 4            
 5         }
 6         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
 7         {
 8                 base.OnNavigatedTo(e);
 9                 MessageBox.Show("OnNavigatedTo");
10         }
11         protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
12         {
13                 base.OnNavigatedFrom(e);
14                 MessageBox.Show("OnNavigatedFrom");
15         }
16         protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
17         {
18                 base.OnNavigatingFrom(e);
19                 MessageBox.Show("OnNavigatingFrom--Cancel");
20         }
21         protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
22         {
23                 base.OnBackKeyPress(e);
24                 MessageBox.Show("OnBackKeyPress");
}

 

 

 

执行的顺序是

加载时

OnNavigatedTo  页面变为活动页面时调用

PhoneApplicationPage_Loaded  

退出时:

OnBackKeyPress 设备硬件按下后退鍵是调用

OnNavigatingFrom--Cancel  刚好在页面不在是活动页面之前时调用

OnNavigatedFrom 在页面不在是活动页面时调用

 

OnBackKeyPress 的妙用:当我们在程序中弹出一个消息框时。如果直接在手机硬件上按后退鍵会直接退出程序,,这个时候我们就可以在OnBackKeyPress 这个方法中把后退的命令改成关闭消息框的命令!

 

服务端跟xaml页面的值传递

我们先简单的配置下服务端Handler.ashx:

 1 string action = context.Request["action"];
 2         if (action == "login")
 3         {
 4             string username = context.Request["username"];
 5             string password = context.Request["password"];
 6             if (username == "admin" && password == "123")
 7             {
 8                 context.Response.Write("ok");
 9             }
10             else
11             {
12                 context.Response.Write("error");
13             }
14         }

简单的设计一个登陆的界面;

给登录注册点击事件

 1  private void button1_Click(object sender, RoutedEventArgs e)
 2         {
 3             WebClient wc = new WebClient();
 4             wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
 5             wc.DownloadStringAsync(new Uri("http://192.168.1.98:1174/WebSite14/Handler.ashx?action=login&username=" + Uri.EscapeDataString(textBox1.Text) + "&password=" + Uri.EscapeDataString(textBox2.Text)));
 6         }
 7 
 8         void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 9         {
10             if (e.Error != null)
11             {
12                 MessageBox.Show("错误");
13             }
14             else
15                 if (e.Result == "ok")
16                 {
17                     MessageBox.Show("登录成功");
18                 }
19                 else
20                     if (e.Result == "error")
21                     {
22                         MessageBox.Show("登录失败");
23                     }
24                     else
25                     {
26                         MessageBox.Show("未知错误");
27                     }
28         }

 这里要注意的是IP地址不能写127.0.0.1 Localhost,因为手机跟电脑连接就自动构成一个局域网,并且手机自己本身就是个小电脑,Localhost就相当于访问手机自己

如果你是用手机跟电脑连接,或者模拟器跟电脑连接,则需要假设IIS,当然端口映射下也行,也可以设置下CassiniDev服务器,设置成any,允许外网访问。

 

假如我们要向手机客户端发送json数据呢。该怎么来接收并解析?

 

 1  string action = context.Request["action"];
 2        
 3         if (action=="test")
 4         {
 5             List<person> list = new List<person>();
 6             list.Add(new person { age=18,name="yzk" });
 7             list.Add(new person { name="zxh",age=17 });
 8             JavaScriptSerializer js=new JavaScriptSerializer();
 9          string json=   js.Serialize(list);
10             context.Response.Write(json);
11         }
12     }
13  
14     public bool IsReusable {
15         get {
16             return false;
17         }
18     }
19     public class person
20     {
21         public string name
22         {
23             get;
24             set;
25         }
26         public int age
27         {
28             get;
29             set;
30         }
31     }

 

 

 

然后我们在客户端接受json数据-在界面上托一个按钮并注册点击事件

 

 1 private void button2_Click(object sender, RoutedEventArgs e)
 2         {
 3             WebClient sc = new WebClient();
 4             sc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(sc_DownloadStringCompleted);
 5             sc.DownloadStringAsync(new Uri("http://192.168.1.98:1174/WebSite14/Handler.ashx?action=test"));
 6         }
 7 
 8         void sc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 9         {
10             if (e.Error != null)
11             {
12                 MessageBox.Show("错误");
13             }
14             else
15             {
16                 string json = e.Result;
17                 List<person> list = new List<person>();
18                 JArray persons = (JArray)JsonConvert.DeserializeObject(json);
19               foreach (var item in persons)
20               {
21                   string name=item["name"].ToString();
22                   int age = int.Parse(item["age"].ToString());
23               }
24             }
25         }
26     }
27     public class person : DependencyObject
28     {
29 
30 
31         public int age
32         {
33             get { return (int)GetValue(ageProperty); }
34             set { SetValue(ageProperty, value); }
35         }
36 
37         // Using a DependencyProperty as the backing store for age.  This enables animation, styling, binding, etc...
38         public static readonly DependencyProperty ageProperty =
39             DependencyProperty.Register("age", typeof(int), typeof(person), null);
40 
41 
42         public string name
43         {
44             get { return (string)GetValue(nameProperty); }
45             set { SetValue(nameProperty, value); }
46         }
47 
48         // Using a DependencyProperty as the backing store for name.  This enables animation, styling, binding, etc...
49         public static readonly DependencyProperty nameProperty =
50             DependencyProperty.Register("name", typeof(string), typeof(person), null);
51 
52 
53 
54     }

 

 

 

这里解析服务端发送过来的数据,我们要引用Newtonsoft.Json.dll来帮我们解析

 

posted @ 2012-08-23 20:37  Carl --卡尔  阅读(1588)  评论(4编辑  收藏  举报