Windows phone 7开发(二.页面间跳转与传值)
来源:互联网 发布:怎么屏蔽网络垃圾短信 编辑:程序博客网 时间:2024/05/21 22:44
Windows Phone7允许创建多个页面,但并不像Windows mobile程序一样,可能关闭窗口或最小化,而是通过跳转的方式来实现的。但是其跳转方式也不同于PC上Sliverlight的跳转方式。不过其传值方式却是一样的。
一、跳转:
可以通过这条语句来完成:this.NavigationService.Naviagte(new Uri("/testPage.xaml", UriKind.Relative));
向前跳转:this.NavigationService.GoFroward();
向后跳转:this.NavigationService.GoBack();
二、QueryString方式:
1中的Button事件处理程序中添加了如下代码:
private void btnMain_Click(object sender, RoutedEventArgs e)
{
string target = "/SubPage.xaml";
target += string.Format("?InputText={0}", txtInput.Text);
this.NavigationService.Navigate(new Uri(target, UriKind.Relative));
}
程序运行时这个Uri是这样的:”/SubPage.xaml?InputText=Hello, Windows Phone 7!” 它的形式和HTML中的查询字符串(Query String)一样,如果要传递多个参数,可以用&来合并,例如:”/SubPage.xaml?InputText=Hello, Windows Phone 7!&Name=Zhang Yin”。
页面2中Button的Click事件处理程序代码如下:
private void btnBack_Click(object sender, RoutedEventArgs e)
{
if (this.NavigationService.CanGoBack)
this.NavigationService.GoBack();
}
并且我在页面2中重写了OnNavigatedTo方法。OnNavigatedTo方法是定义在Page类中的,所以PhoneApplicationPage也继承了此方法,它是一个Virtual方法,所以需要用override关键字重写。OnNavigatedTo在页面被创建后马上会被调用,也就是说在OnNavigatedTo 方法被调用时,页面的构造函数已经执行完毕了。类似的还有OnNavigatedFrom方法,它是在离开当前页面时触发的。
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
Page类定义了一个类型为NavigationContext的NavigationContext属性,它包含一个类型为IDictionary<string, string>的QueryString属性。判空检测后,将InputText键对应的值赋给文本框的Text属性。最后触发基类的OnNavigatedTo方法以保证基类的功能正常。当然也可以使用下面的代码,效果是一样的:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
string value = string.Empty;
IDictionary<string, string> queryString = this.NavigationContext.QueryString;
queryString.TryGetValue("InputText", out value);
if (value != null)
{
txt.Text = value;
}
base.OnNavigatedTo(e);
}
通过QueryString虽然可以向一个页面传递参数,但却无法回传数据,例如我在页面2中将文本修改为“Windows Phone 7!”,然后点击“前往第1页”按钮,得到的还是初始的那个页面,如下图:
要解决这个问题,就需要使用全局变量了。
三、全局变量
对于全局变量来说,我们可以在项目中添加一个在整个程序生命周期中都能访问到的类,将相应的数据保存在这个类的属性里。不过现在我们不用自己动手做这些额外的工作了。项目中本身就有一个类满足这个需求:App类。 App 类继承自Application类。通过Application的Current属性就可以获取到与当前程序关联的Application类实例,然后通 过转换就可以得到App类实例。程序中所有的页面都可以访问到这个App类,所以将我们要保存的数据放到App类中是最好不过了,免去了自己实现的麻烦。在App类中添加一个公有的InputText属性(C# 3.0的自动属性) :
public partial class App : Application
{
public string InputText { get; set; }
……
}
页面1中的代码如下:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
App app = Application.Current as App;
if (app.InputText != null)
txtPage1.Text = app.InputText;
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
App app = Application.Current as App;
app.InputText = txtPage1.Text;
base.OnNavigatedFrom(e);
}
- 一起学Windows phone 7开发(二.页面间跳转与传值)
- Windows phone 7开发(二.页面间跳转与传值)
- 一起学Windows phone 7开发(二.页面间跳转与传值)
- Windows phone 7开发之(页面间跳转与传值)
- windows phone 7 页面间跳转方法
- Windows Phone 页面间跳转
- Windows Phone 7 - 页面跳转
- win phone 8 页面间跳转与传值
- windows phone 页面传值(7)
- Windows Phone 7两个页面动画跳转
- Windows Phone页面跳转 导航
- Windows Phone 7应用程序开发(二)Hello Windows Phone(5)
- windows phone 8 mvvmlight页面跳转类(NavigateCommand)
- Windows phone开发初体验之(二)-页面导航
- Windows Phone 页面跳转事件调用顺序
- windows phone关于页面跳转的总结
- Windows Phone 7应用程序开发(二)Hello widnows Phone(4)
- windows phone 页面之间传值方式
- Rails3 入门之六 建立一个资源
- 递归输出全排列
- linux系统安装tomcat
- 编程规则 - 3 类设计规则 类设计的基本要求 3.2
- fopen打开文件"r"和"rb"格式的区别
- Windows phone 7开发(二.页面间跳转与传值)
- Java Serializable
- fseek函数
- Ubuntu 安装JDK
- c语言中dat 和txt的区别
- Select For Update行级锁定
- hdu 2828
- c# 数据类型 小结(未完)
- think different