Windows Phone 编程中页面间传值方法

WP开发过程中有时会遇到页面间转值的需求,如果定义两个页面,一个是初始页面Source Page,另外一个是跳转的页面Destination Page,简单地分析主要有两个方面的要求:: r2 q. H% e" F4 F4 i2 t! M
• 首先是在source page跳转到destination page时给destination page页面传值的实现;
• 然后是当在destination page中调用goback函数回到source page时如何在source page传值;) c9 m. g: `: |; X3 \; R9 f7 w
) Z3 A' X* g3 Y; x) t
第一点系统本身提供了基本的实现方法,新建一个项目DataPassingDemo,然后新建一个页面SecondPage.xaml,我们需要实现就是从MainPage中跳转到SecondPage中去,并传递参数让SecendPage捕捉。首先在Mainpage中增加一个Textblock并且增加事件处理函数:2 O* v; xi3 x_
view sourceprint?: b3 y5 ^4 mJ9 k; n
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Navigate to 2nd page with data" HorizontalAlignment="Center"
VerticalAlignment="Center" Padding="0 34" * F/ v" T% g8 R( o+ w$ k
ManipulationStarted="TextBlock_ManipulationStarted"/>
</Grid># Q2 z& X( @. gt2 |

在Mainpage的后台代码中,实现TextBlock_ManipulationStarted方法如下:4 ]7 Z) j8 b- P. W3 W! f
view sourceprint?
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs args)) B, q4 u" t" `8 ]w( h
{
string destination = "/SecondPage.xaml?parameter1=hello&parameter2=world";" B( k6 }+ w3 \" C/ lR. a: L. q
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative)); 9 ?7 ^$ A- sA% V
args.Complete(); * o' t$ I) S3 D
args.Handled = true; 0 J, K8 g$ }0 b; r% \$ ~* a1 |+ R, e
}% B& U4 o- q+ G1 P
可以看到上面的那个destination是不是很像网页间传递参数的形式呢?同理在SecondPage中增加一个Textblock,并给该Textblock的ManipulationStarted事件中增加Goback()事件。同时,为了捕捉MainPage传递过来的参数,在SecondPage的后台代码中实现下面的代码:
view sourceprint?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs args)
{
IDictionary<string, string> parameters = this.NavigationContext.QueryString;/ u1 \' J" W' `6 M9 i
if (parameters.ContainsKey("parameter1"))% ~( H4 I8 L7 x' ]
{' t, ?O' ^0 t
string parameter1 = parameters["parameter1"];
string parameter2 = parameters["parameter2"];' N/ V% G6 K+ \
txtblk.Text = String.Format("Parameter1 is:{0} and parameter2 is:{1}", parameter1, parameter2);) F8 y- v, v# C: I3 @V
}" \4 Q0 h, u* h7 G; t
base.OnNavigatedTo(args);2 f" X; y. U* t% ^% V; f

}

通过重载OnNavigatedTo函数实现获取传递过来的参数并在其中的一个TextBlock中显示出来。
[local]1[/loca所以实现第一个传值要求的方法很简单,只要通过给NavigationService的目标页面地址附带上参数再由目标页面获取参数即可,而我们要注意的地方是,由于移动设备设计的便捷性要求,我们应该避免那些很复杂的传递参数的设计,并且,在设计时要注意Windows Phone设计中的墓碑机制,才能设计出合理高效的WP应用。8 e' _) q! Z6 b; f# w+ m8 L6 w
7 R+ i% l( X' [: M, S3 O, M
接着我们来考虑第二个问题,如何在页面间共享,传递数据。我们可以考虑到如果有一个是中间的“容器”可以存放一些公共的数据的话那且不是可以实现这个要求了吗?这时如果熟悉Silverlight设计的话头脑里就会呈现出App这个类,由于所有的页面都可以访问到App这个类,所以我们可以把一些准备共享的数据放在App这个类中定义。就在上面那个例子中,我们在App类中增加一个公共变量:
view sourceprint?
public string SharedString { set; get; }
7 l7 c) X- C) W, c6 G4 _
这时如果想在MainPage中给SecondPage传递参数的话则需要先访问那个共享数据,这时的MainPage中的后台代码如下:
view sourceprint?5 T) m. \& ?2 h
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs args)$ |8 m* H2 P) O/ P8 a5 T, c* [
{
(Application.Current as App).SharedString = "Hello World";
this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));: S/ q1 Q" v4 V; k3 {/ T
args.Complete();; H8 k. R1 n& t8 Z/ o
}

即在访问SecondPage前先修改那个共享数据的值,然后在SecondPage的OnNavigatedTo事件中代码修改如下:
view sourceprint?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs args)0 M# I6 s) F3 U. _/ dT: t# u; @
{! ^2 V% h8 b3 W. I' k# w+ O+ H
string sharedString = (Application.Current as App).SharedString;# _, i+ L1 R6 R8 q
txtblk.Text = sharedString;
base.OnNavigatedTo(args);
}0 U3 K7 x0 U8 }' w7 h% G
% I5 w+ Y3 Q' R! ~4 k8 @
同理,如果想通过SecondPage向MainPage传递数据的话,只要在调用GoBack函数前修改那个共享数据的值再由MainPage中的OnNavigatedTo函数来获取到相对应的数据即可。5 r+ A0 N) K5 f
% K3 Z8 f' A9 J2 Y) W
到这里我们已经基本可以实现上面的两个要求了,但第二种方法只是一种取巧的方法,在逻辑及实现上都有不合理的地方,我们应该走思考另外一种更为合理与通用的实现方式,那就是OnNavigatedFrom这个函数了。大家可能会想,from不是很明显吗,我们就是通过from的原页面跳到目标页面的,那么这个from有何用处呢。其实它的用处挺大的,例如,通过这个函数我们可以很好的解决上面提到的问题。
2 K% U( n* S- g- S5 v0 S
最后用一个例子去说明这种方式的具体实现,我们定义两个页面,和之前的类似,这次我们通过SecondPage返回的值去定义MainPage页面的颜色,MainPage的后台代码定义如下:! z4 X" q( n- n% I4 T
view sourceprint?$ @, g, L) D4 o$ q$ H8 K
public partial class MainPage : PhoneApplicationPage
{bn& S' ^; _6 J8 l
public MainPage()+ B! _1 mA' R7 n6 {5 r7 `$ d
{
InitializeComponent();v, f" i: m: n3 k" Q
}- t8 \* P/ o' e* f4 z, g8 B
public Color? ReturnedColor { set; get; }
# w7 u/ v. \" l- B7 R. v
private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs args)
{
this.NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));- x{$ o5 s0 a5 |& ]: O, }

args.Complete();
args.Handled = true;; C" R5 y1 g% }. q/ g/ h6 A: x# ?
}% V( C' P- l6 X$ l- ?
}7 ^* v- _v, I
[+ FM" \{
这里定义为Color?,因为返回的值有可能是非颜色的。而SecondPage中的后台代码定义如下:
show sourceview sourceprint?& e8 c4 B" {( U
namespace DataPassingDemo
{
public partial class SecondPage : PhoneApplicationPage8 @$ U1 K2 I& C! L0 s! y; w
{3 U- t- c9 y! r
public SecondPage()+ v8 C. MF. s! E& g/ M, h
{# v5 q; W( q7 J2 @
InitializeComponent();; k, UB5 H4 Q7 R( L/ B, T
}6 w2 V) `+ A, f
Random rand = new Random();

private void TextBlock_ManipulationStarted(object sender, ManipulationStartedEventArgs args)
{( Q3 o8 Z. p3 l. a
this.NavigationService.GoBack();
args.Complete();
args.Handled = true;
}
* J2 V; A. t; \2 y) U9 `" F
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)' s0 n0 R, S( E; a$ Q' S8 |
{
if (ContentPanel.Background is SolidColorBrush)) p$ }/ f5 B# ?! A7 [1 J
{
Color clr = (ContentPanel.Background as SolidColorBrush).Color;
if (e.Content is MainPage)
(e.Content as MainPage).ReturnedColor = clr;. R2 R% U* i2 p+ c" G( E
}
base.OnNavigatedFrom(e);4 ]2 `1 r2 ?4 e+ O
}! @9 u! {5 _2 u" D( {' Z
8 s) U4 UM( A' E+ p
protected override void OnManipulationStarted(ManipulationStartedEventArgs args)) Z. T- M) G3 a2 a' A) \+ Y
{
ContentPanel.Background = new SolidColorBrush(/ b( \! H4 n; M, z4 p7 l+ w5 O; g
Color.FromArgb(255, (byte)rand.Next(255),* K! u$ ]- a" M, w
(byte)rand.Next(255),
(byte)rand.Next(255)));5 O1 J) R% U. H; c
base.OnManipulationStarted(args);4 S' P& _' R, c# K, R2 ^7 \5 T
}
}q0 K; N$ W9 O+ w% y4 F
}/ ]5 L; g9 e, R
4 G, N, o- {1 R3 f. d" K* j
我们通过获得一个随机数值组合而成的颜色设置为SecondPage的背景颜色,然后通过OnNavigatedFrom设置ReturnedColor为当前背景颜色,所以为了获取SecondPage返回的ReturnedColor,在MainPage的后台代码中还需要重载OnNavigatedTo方法响应这个OnNavigatedFrom:
view sourceprint?
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs args)
{
if (ReturnedColor != null)
ContentPanel.Background =
new SolidColorBrush(ReturnedColor.Value);
base.OnNavigatedTo(args);( f& J8 ~H9 s& \9 |
}

通过OnNavigatedFrom与OnNavigatedTo,我们就完成了数据的传递过程。l]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值