windows phone页面间传递数据

 页面间传递数据包括两个问题
1、如何从源页面传递数据到目标页面?
下面例子提供解决上面问题的方案。
源页面MainPage.xaml内容区域包含一个TextBlock,如
<TextBlock HorizontalAlignment="Center" Name="txt1" Text="navigate to 2nd page" VerticalAlignment="Center" ManipulationStarted="txt1_ManipulationStarted" />
MainPage.xaml.cs代码如下所示:
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
String destination = "/Page1.xaml";
if (this.ContentPanel.Background is SolidColorBrush)
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;
destination += String.Format("?Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B);
}
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面
e.Complete();
e.Handled = true;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
base.OnManipulationStarted(e);
}
}
}
目标页面Page1.xaml代码重写了OnNavigatedTo方法,如下所示:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//当该函数被调用时,页面的构造函数已经执行完毕,但是还没有执行其他的方法
{
IDictionary<String, String> parameters = this.NavigationContext.QueryString;
if (parameters.ContainsKey("Red"))
{
byte R = Byte.Parse(parameters["Red"]);
byte G = Byte.Parse(parameters["Green"]);
byte B = Byte.Parse(parameters["Blue"]);
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,R,G,B));
}
base.OnNavigatedTo(e);
}
运行程序,从MainPage导航到Page1,你会发现背景颜色是相同的。
2、当回到源页面时,如何返回数据?
windows phone为我们提供了专门解决第一个问题的方案,但是还没有解决第二个问题的现成方案。下面例子是一个折中的方案,可参考。
上面MainPage.xaml.cs可改成
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage{
Random rand = new Random();
public Color? ReturnedColor{set;get;}//这是一个可空的(nullable)Color对象,用来保存想要返回的数据(颜色)
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void txt1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
String destination = "/Page1.xaml";
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));//导航至指定页面
e.Complete();
e.Handled = true;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
base.OnManipulationStarted(e);
}
protected override void OnNavigateTo(NavigationEventArgs e)//当构造函数执行完毕且还没有执行其他函数时(自动)调用
{
if(this.ReturnedColor != null)
{
this.ContentPanel.Background = new SolidColorBrush(this.ReturnedColor.Value);
}
base.OnNavigateTo(e);
}

 

}
}

Page1.xaml.cs可改成
namespace PhoneApp2
{
public partial class Page1 : PhoneApplicationPage
{
Random rand = new Random();
public Color? ReturnedColor{set;get;}//这是一个可空的(nullable)Color对象,用来保存想要返回的数据(颜色)
// 构造函数
public Page1()
{
InitializeComponent();
}
private void txt2_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
this.NavigationService.Goback();//导航返回
e.Complete();
e.Handled = true;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{//当触摸到页面里textblock以外的部分时,contentpanel的背景会变成随机颜色。
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255, (byte)rand.Next(255), (byte)rand.Next(255), (byte)rand.Next(255)));//设置背景颜色
base.OnManipulationStarted(e);
}
protected override void OnNavigateFrom(NavigationEventArgs e)
{
if(this.ContentPanel.Background is SolidColorBrush)
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;
if(e.Content is MainPage)
(e.Content as MainPage).ReturnedColor = clr;//在导航出Page1页面时设置返回值并保存到MainPage变量中
}
base.OnNavigateFrom(e);
}
}
}
-参数NavigationEventArgs类定义了两个属性:Uri类型的Uri和object类型的Content。
-当MainPage使用参数"/Page1.xaml"来调用Navigate()函数时,MainPage中的OnNavigateFrom()方法被调用,调用时参数Uri属性为"/Page1.xaml",Content属性类型为Page1。这是一人新创建的Page1实例。随后Page1调用的OnNavigateTo()也使用同样的事件参数来标识值为"/Page1.xaml"的Uri对象以及值为Page1的Content对象。

时间: 2024-08-17 15:02:42

windows phone页面间传递数据的相关文章

浅谈ASP中Web页面间的数据传递

web|数据|页面 基于Web的动态网页设计必会涉及到页面间的数据传递,文章探讨了ASP设计中常用的Web页面间的数据传递方式,分析各种数据传递方式的使用方法.使用场合及优缺点,其都是设计阶段选择数据传递方式考虑的关键.往往使用动态网页技术制作ASP应用程序时一般至少拥有二个或二个以上的Web页面,这时就得考虑在多个Web页面间传递数据的处理工作.而ASP应用程序的各个页面类似于Windows应用程序的form窗体,Windows应用程序各form间数据传递可以通过定义全局变量等方法来实现.网页

ASP中Web页面间的数据传递方式

摘要:基于web的动态网页设计必会涉及到页面间的数据传递,文章探讨了asp设计中常用的web页面间的数据传递方式,分析各种数据传递方式的使用方法.使用场合及优缺点,其都是设计阶段选择数据传递方式考虑的关键. 关键词 数据传递变量浏览器端网页 往往使用动态网页技术制作asp应用程序时一般至少拥有二个或二个以上的web页面,这时就得考虑在多个web页面间传递数据的处理工作.而asp应用程序的各个页面类似于windows应用程序的form窗体,windows应用程序各form间数据传递可以通过定义全局

js-两个HTML页面如何传递数据?

问题描述 两个HTML页面如何传递数据? 这是A html页面,通过?的方式将数据传递给B页面这是B html页面,接受到d数据,但是d是name=[][][]:中括号里面数据是我想要的,我该怎么取到这些数据?我这样在页面间传递数据的方法正确吗?求各位大神开导开导小弟啊 解决方案 浏览器支持localStorage可以放缓存里面用到sessionStorage也可以放在sessionStorage里面存储:localStorage.setItem(""temp""J

《React Native移动开发实战》一一3.8 实现页面间的数据传递

3.8 实现页面间的数据传递跳转和返回的效果实现了,那么,如何实现页面间的数据传递和通信呢?其实,从上述代码中读者已经可以发现:React Native使用props来实现页面间数据传递和通信.在React Native中,有两种方式可以存储和传递数据,即props(属性)以及state(状态),其中:? props通常是在父组件中指定的,而且一经指定,在被指定的组件的生命周期中则不再改变.? state通常是用于存储需要改变的数据,并且当state数据发生更新时,React Native会刷新

windows phone页面间数据共享

 可以通过静态属性Application.Current可以返回当前程序的Application对象,然后可以简单地将其转换成App类型.这意味着可以使用App类来存储用于程序中多个页面共享的数据.下面例子演示如何利用App类实现页面间数据共享:在Silverlight项目的App类定义一个简单的公共属性:public partial class App : Application{//用于在页面间共享数据的公共属性public Color? SharedColor { set; get; }/

各位大神们,求助求助!窗口间传递数据!!

问题描述 本人只学习过C语言,对.NET几乎小白.自己看了些资料做了个软件,是毕业设计!现在需要在两个窗口间传递数据,查看了好多资料看的我云里雾里,没办法,实在是不懂!希望从Form1中将数组传递到Form2中去,我把(自己认为的)一些没必要的代码我就删去了,免得大家看的麻烦!就是将Form1最下面申请的buf内存地址传递给Form2中最下面的huidu数组,只要告诉我怎么写代码就可以了,顺便说出方法就更好了,谢谢!对了,Form1和Form2不是MDI父子关系,因为设成MDI父子关系,Form

PHP页面间传递值和保持值的方法_php实例

一.目录结构 二.两次页面间传递值 在两次页面之间传递少量数据,可以使用get提交,也可以使用post提交,二者的区别恕不赘述. 1.get提交 使用get提交来传递数据,在链接地址中修改发送到服务器的 URL 如下所示http://www.cnblogs.com/MarkRao/p/php01.html?gName=mark&gAge=26,当然也可以在表单中设置method="get",php中接收get提交过来的数据值,使用预定义$_GET变量 从带有 GET 方法的表单

C#中使用SendMessage在进程间传递数据的实例

原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Sender. 其中,CopyDataStruct项目的输出类型为"类库",Receiver和Sender项目的输出类型为"Windows 应用程序". 整个实例程序的结构如下图所示.   2 CopyDataStruct项目实现 定义结构体COPYDATASTRUCT,代码如

一起谈.NET技术,在ASP.NET网页间传递数据的五种方法

重点总结 目前为止在ASP.NET网页中传递数据的方式至少有5种: 1.通过查询字符串传递数据. 2.通过HTTP POST传递数据. 3.通过会话状态传递数据. 4.通过源页的公共属性传递数据. 5.通过源页中的控件值传递数据. 到底使用哪种方式来进行数据的传递,这可能受到两方面的影响: 1.页面重定向的方式. 2.源页和目标页是否位于相同的ASP.NET应用程序中. 如果源页和目标页位于不同的ASP.NET应用程序中则只能通过查询字符串和HTTP POST传递数据. 而如果源页和目标页位于相