Windows Phone开发之路(16) 如何在页面间传递数据

  这一篇文章要解决的问题是如何从源页面传递数据到目标页面。其实Windows Phone已经为我们提供了一套解决方案,那就是查询字符串。

  下面这个项目要实现的效果是:当从MainPage页面导航到SecondPage时,该项目为SecondPage提供了MainPage当前的背景色,而且SecondPage也把自己初始化成这种颜色。这里传递的数据是背景颜色值。

  MainPage.xaml XAML代码:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to Second Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  MainPage.xaml C#代码:

namespace SilverlightPassData
{
public partial class MainPage : PhoneApplicationPage
{
Random rand = new Random();

// 构造函数
public MainPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理Manipulation事件
{
//todo:导航到Second Page页面,并且传递页面背景颜色信息给Second Page.
string destination = "/SecondPage.xaml";//创建一个String变量保存目标页面地址

if (this.ContentPanel.Background is SolidColorBrush)//检查能否成功转换成SolidColorBrush类型。Background属性为Brush类型
{
Color clr = (this.ContentPanel.Background as SolidColorBrush).Color;//将Background属性值转换成SolidColorBrush类型并获取颜色值
destination += string.Format("?Red={0}&Green={1}&Blue={2}",clr.R,clr.G,clr.B);//将颜色值作为参数附在目标地址后,类似于HTML查询字符串格式
}

this.NavigationService.Navigate(new Uri(destination,UriKind.Relative));//导航到目标地址

e.Complete();//表示不再处理其它Manipulation事件
e.Handled = true;//将路由事件标记为已处理
}

protected override void OnManipulationStarted(ManipulationStartedEventArgs e)//重写基类Control的虚方法
{
//todo:实现当单击屏幕的时候随机改变页面背景色
this.ContentPanel.Background = new SolidColorBrush(Color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256),(byte)rand.Next(256)));

base.OnManipulationStarted(e);//基类访问表达式调用基类方法
}
}
}

  

  SecondPage.xaml XAML代码:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Go back to Main Page!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="0,34"
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid>

  SecondPage.xaml C#代码:

namespace SilverlightPassData
{
public partial class SecondPage : PhoneApplicationPage
{
public SecondPage()
{
InitializeComponent();
}

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs e)//处理Manipulation事件
{
//todo:返回或导航到MainPage页面
this.NavigationService.GoBack();

e.Complete();
e.Handled = true;
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)//重写基类Page的虚方法,当页面变为框架的活动页面时调用
{
//todo:获取从MainPage传递过来的数据,并以此来初始化页面背景颜色
IDictionary<string, string> parameters = this.NavigationContext.QueryString;//创建泛型接口字典来接受返回结果对象

if (parameters.ContainsKey("Red"))//调用ContainsKey()方法判断结果对象中是否包含Red的键
{
//todo:分别获取每个键的值并转换为byte类型
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);//调用基类虚方法
}
}
}

  效果如图:

           

  下一篇将要总结的是关于如何在页面间数据共享。

原文地址:https://www.cnblogs.com/mcgrady/p/2346755.html