Windows phone 8 页面间数据共享方式

网上转的,方便自己以后学习。

 页面间传递数据包括两个问题
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对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值