在上一节我们知道了如何处理WindowsPhone的页面导航同时也实现了两个页面之间的数据传递。在实际开发中我们还需要为两个页面传递数据。经过看官方文档和网上资料搜集,总结参数传递主要有以下的四种方式:
1、通过NavigationContext的QueryString方式;
2、通过程序的App类设置全局变量(此方法可以传递对象);
3、通过NavigationEventArgs事件类的Content属性设置;
4、通过PhoneApplicationService类的State属性。
通过NavigationContext的QueryString方式这种方式在上一节已经介绍了,在些不再描述。下面重点介绍后面三种
(一) 通过程序的App类设置全局变量(此方法可以传递对象)
在工程目录下新建一个”Model”的文件夹,再在这个文件夹里新建一个类:Person类
public class Person
{
public String Name { get;set; }
public int Age { get; set; }
}
然后再在App.xaml.cs文件中加上这么一段代码
public partial class App : Application
{
///<summary>
///提供对电话应用程序的根框架的轻松访问。
///</summary>
///<returns>电话应用程序的根框架。</returns>
public PhoneApplicationFrame RootFrame { get; private set; }
public static Personperson { get; set;} ……..
}
再在新建的AppHome.xaml.cs文件中为页面上的按钮添加click事件
private voidbutton1_Click(object sender, RoutedEventArgs e)
{
App.person= new Model.Person
{
Name=txt_Name.Text,
Age=Convert.ToInt32(txt_Age.Text)
};
Uriuri = new Uri("/AppData.xaml",UriKind.Relative);
NavigationService.Navigate(uri);
}
然后在AppData.xaml文件的Loaded事件中接受传递过来的值:
private voidLayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
if(App.person!=null)
{
tb_Name.Text = App.person.Name;
tb_Age.Text = App.person.Age.ToString();
}
}
最终实现效果:
(一)通过NavigationEventArgs事件类的Content属性设置
ContentPage1.xaml.cs文件中:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
vartargetPage = e.Content as ContentPage2;
if(targetPage != null)
{
targetPage.StrContent=textBox1.Text;
}
}
privatevoid button1_Click(objectsender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/ContentPage2.xaml",UriKind.Relative));
}
ContentPage2.xaml.cs中取出Content的值:
public String StrContent { get;set; }
protectedoverride voidOnNavigatedTo(System.Windows.Navigation.NavigationEventArgse)
{
if(StrContent != null)
{
this.tb_ContentValue.Text= StrContent;
}
}
实现的效果:
(二)通过PhoneApplicationService类的State属性:
StatePage1.xaml.cs文件:
protected override voidOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgse)
{
PhoneApplicationServicephoneService = PhoneApplicationService.Current;
phoneService.State["param1"] = this.textBox1.Text;
}
privatevoid button1_Click(objectsender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/StatePage2.xaml",UriKind.Relative));
}
StatePage2.xaml.cs文件:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if(PhoneApplicationService.Current.State.ContainsKey("param1"))
{
this.tb_ParamValue.Text= PhoneApplicationService.Current.State["param1"] asstring;
}
}
privatevoid button1_Click(objectsender, RoutedEventArgs e)
{
if(NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
}
实现效果:
如需转载引用请注明出处:http://blog.csdn.net/jiahui524
源代码下载地址:http://download.csdn.net/detail/jiahui524/4316106
分享到:
相关推荐
Windowsphone开发初体验之(三)-参数传递 源代码
Windows Phone开发(六)-- 多任务之墓碑机制 代码 http://blog.csdn.net/cc_net/article/details/6665737
Windows Phone开发(五)-- 导航控制 . http://blog.csdn.net/cc_net/article/details/6660889的Demo
Windows Phone开发(四)-- 导航事件和传值 代码http://blog.csdn.net/cc_net/article/details/6581281
Windows Phone7手机开发基础实例--手机归属地
Windows Phone开发-XNA基础
ArcGIS-API-for-Windows-Phone开发实例 ArcGIS-API-for-Windows-Phone开发实例 ArcGIS-API-for-Windows-Phone开发实例
windows phone 开发 在线教程
Windows_Phone_7入门经典-使用Silverlight和XNA开发Windows_Phone应用
windows phone 8 silverlight XAML
[Packt Publishing] Windows Phone 8 游戏开发教程 (英文版) [Packt Publishing] Windows Phone 8 Game Development (E-Book) ☆ 图书概要:☆ A practical guide to creating games for the Windows Phone 8 ...
windows phone8开发源代码 六:数据存储 七:图形动画 八:多媒体 九:启动器与选择器 十:手机感应编程
WindowsPhone开发,包含聊天室案例开始,阅读器、天气预报、SignalR
Windows Phone 8 JumpStart Module 16 In-App Purchasing Demo
windows phone 开发书籍及资料
Windows Phone Toolkit Aug 2011 in depth-v1.PDF 这个是本好书,将部分控件封装了,部署在手机上体验了一下那些效果还是相当不错的!
Developers are racing to discover how to develop for Windows Phone 7—and there is no quicker resource to help you get up to speed than Windows Phone 7 Recipes. This book covers all aspects of ...
一起学Windows Phone7开发课程,对Windows Phone7各个功能模块开发都有所讨论,是一个由入门到深入的系列课程,是一个面向开发者和对Windows Phone感兴趣人士的课程,通过这个系列课程的学习,可以使您对Windows ...
一起学Windows Phone7开发课程,对Windows Phone7各个功能模块开发都有所讨论,是一个由入门到深入的系列课程,是一个面向开发者和对Windows Phone感兴趣人士的课程,通过这个系列课程的学习,可以使您对Windows ...
翻译Windows phone 7应用市场提交过程 - App Hub Application Submission Walkthrough.附加图片介绍。