xaml页跟xaml页之间的值传递:
向Page1页面中传递值:
代码如下 | 复制代码 |
NavigationService.Navigate(new Uri("/Page1.xaml?name=" + txtName.Text, UriKind.Relative)); |
在page1中接受传递过来的值
代码如下 | 复制代码 |
textBlock1.Text= NavigationContext.QueryString["name"]; |
当我们传递比较复杂的值的时候 就不能像上面那样简单的传递,比如a=b&c=d,我们就要考虑用EscapeDataString来进行转义后在传递
代码如下 | 复制代码 |
NavigationService.Navigate(new Uri("/Page1.xaml?name="+Uri.EscapeDataString("a=b&c=d") , UriKind.Relative)); |
当我们需要传递一个比上面还要复杂的对象怎么办呢?比如我们需要传递的是一个Buttun按钮
代码如下 | 复制代码 |
private void button1_Click(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/Page1.xaml?name="+Uri.EscapeDataString("a=b&c=d") , UriKind.Relative)); Page1.btn = button1; } public static Button btn; private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { textBlock1.Text = (string)btn.Content; }
|
这样就把前一个页面的Button按钮对象传递过来了 !非常的灵活
代码如下 | 复制代码 |
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show("PhoneApplicationPage_Loaded"); } protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); MessageBox.Show("OnNavigatedTo"); } protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedFrom(e); MessageBox.Show("OnNavigatedFrom"); } protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); MessageBox.Show("OnNavigatingFrom--Cancel"); } protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) { base.OnBackKeyPress(e); MessageBox.Show("OnBackKeyPress"); } |
执行的顺序是
加载时
OnNavigatedTo 页面变为活动页面时调用
PhoneApplicationPage_Loaded
退出时:
OnBackKeyPress 设备硬件按下后退?是调用
OnNavigatingFrom--Cancel 刚好在页面不在是活动页面之前时调用
OnNavigatedFrom 在页面不在是活动页面时调用
OnBackKeyPress 的妙用:当我们在程序中弹出一个消息框时。如果直接在手机硬件上按后退?会直接退出程序,,这个时候我们就可以在OnBackKeyPress 这个方法中把后退的命令改成关闭消息框的命令!
服务端跟xaml页面的值传递
我们先简单的配置下服务端Handler.ashx:
代码如下 | 复制代码 |
string action = context.Request["action"]; if (action == "login") { string username = context.Request["username"]; string password = context.Request["password"]; if (username == "admin" && password == "123") { context.Response.Write("ok"); } else { context.Response.Write("error"); } } |
简单的设计一个登陆的界面;
给登录注册点击事件
代码如下 | 复制代码 |
private void button1_Click(object sender, RoutedEventArgs e) { WebClient wc = new WebClient(); wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 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))); } void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("错误"); } else if (e.Result == "ok") { MessageBox.Show("登录成功"); } else if (e.Result == "error") { MessageBox.Show("登录失败"); } else { MessageBox.Show("未知错误"); } } |
这里要注意的是IP地址不能写127.0.0.1 或Localhost,因为手机跟电脑连接就自动构成一个局域网,并且手机自己本身就是个小电脑,Localhost就相当于访问手机自己
如果你是用手机跟电脑连接,或者模拟器跟电脑连接,则需要假设IIS,当然端口映射下也行,也可以设置下CassiniDev服务器,设置成any,允许外网访问。
假如我们要向手机客户端发送json数据呢。该怎么来接收并解析?
代码如下 | 复制代码 |
string action = context.Request["action"]; if (action=="test") { List<person> list = new List<person>(); list.Add(new person { age=18,name="yzk" }); list.Add(new person { name="zxh",age=17 }); JavaScriptSerializer js=new JavaScriptSerializer(); string json= js.Serialize(list); context.Response.Write(json); } } public bool IsReusable { get { return false; } } public class person { public string name { get; set; } public int age { get; set; } } |
然后我们在客户端接受json数据-在界面上托一个按钮并注册点击事件
代码如下 | 复制代码 |
private void button2_Click(object sender, RoutedEventArgs e) { WebClient sc = new WebClient(); sc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(sc_DownloadStringCompleted); sc.DownloadStringAsync(new Uri("http://192.168.1.98:1174/WebSite14/Handler.ashx?action=test")); } void sc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show("错误"); } else { string json = e.Result; List<person> list = new List<person>(); JArray persons = (JArray)JsonConvert.DeserializeObject(json); foreach (var item in persons) { string name=item["name"].ToString(); int age = int.Parse(item["age"].ToString()); } } } } public class person : DependencyObject { public int age { get { return (int)GetValue(ageProperty); } set { SetValue(ageProperty, value); } } // Using a DependencyProperty as the backing store for age. This enables animation, styling, binding, etc... public static readonly DependencyProperty ageProperty = DependencyProperty.Register("age", typeof(int), typeof(person), null); public string name { get { return (string)GetValue(nameProperty); } set { SetValue(nameProperty, value); } } // Using a DependencyProperty as the backing store for name. This enables animation, styling, binding, etc... public static readonly DependencyProperty nameProperty = DependencyProperty.Register("name", typeof(string), typeof(person), null); }
|
这里解析服务端发送过来的数据,我们要引用Newtonsoft.Json.dll来帮我们解析