silverlight,frame page QueryString及uri映射

silverlight中frame相当一个框架似的,可提供在不同*.xaml之间的导航,frame显示的内容就是一个一个的page

类似*.aspx一样。请注意frame导航的是page而不是userControl,所以在新建的时候记得选Silveright Page.

在不同page导航的时候可以像aspx一样传递QueryString不过写法有点改变而已。

uri映射就不多说了,其实也是就虚拟的uri.

示例代码:

MainPage.xaml

代码
   
     
1 < UserControl
2 xmlns:my ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
3 xmlns:windowsNav ="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
4 x:Class ="FramePageDemo.MainPage"
5 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
6 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
7 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 mc:Ignorable ="d"
10 d:DesignHeight ="300" d:DesignWidth ="400" >
11
12 < Grid x:Name ="LayoutRoot" Background ="White" >
13 < StackPanel >
14 < Border BorderBrush ="Red" BorderThickness ="1" >
15 < my:Frame x:Name ="frame" Width ="200" Height ="200" Source ="/Page1.xaml" >
16 < my:Frame.UriMapper >
17 < windowsNav:UriMapper >
18 < windowsNav:UriMapping Uri ="/p1" MappedUri ="/Page1.xaml" ></ windowsNav:UriMapping >
19 < windowsNav:UriMapping Uri ="/p2" MappedUri ="/Page2.xaml" ></ windowsNav:UriMapping >
20 </ windowsNav:UriMapper >
21 </ my:Frame.UriMapper >
22 </ my:Frame >
23 </ Border >
24 < Button Content ="Page1" Click ="Button_Click" />
25 < Button Content ="Page2" Click ="Button_Click_1" />
26 </ StackPanel >
27 </ Grid >
28   </ UserControl >
29  

 

代码
   
     
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Net;
5   using System.Windows;
6   using System.Windows.Controls;
7   using System.Windows.Documents;
8   using System.Windows.Input;
9   using System.Windows.Media;
10   using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12
13 namespace FramePageDemo
14 {
15 public partial class MainPage : UserControl
16 {
17 public MainPage()
18 {
19 InitializeComponent();
20 }
21
22 private void Button_Click( object sender, RoutedEventArgs e)
23 {
24 frame.Source = new Uri( " /Page1.xaml?date= " + DateTime.Now.Ticks.ToString(),UriKind.Relative);
25 }
26
27 private void Button_Click_1( object sender, RoutedEventArgs e)
28 {
29 frame.Source = new Uri( " /Page2.xaml?date= " + DateTime.Now.Ticks.ToString(), UriKind.Relative);
30 }
31 }
32 }
33

 

 

Page1.xaml

 

代码
   
     
1 < navigation:Page x:Class ="FramePageDemo.Page1"
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable ="d"
7 xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
8 d:DesignWidth ="640" d:DesignHeight ="480"
9 Title ="Page1 Page" >
10 < Grid x:Name ="LayoutRoot" >
11 < TextBlock Text ="page1" />
12 </ Grid >
13 </ navigation:Page >
14

 

 

Page2.xaml

 

代码
   
     
1 < navigation:Page xmlns:my ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input" x:Class ="FramePageDemo.Page2"
2 xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d ="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable ="d"
7 xmlns:navigation ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
8 d:DesignWidth ="640" d:DesignHeight ="480"
9 Title ="Page2 Page" Loaded ="Page_Loaded" >
10 < Grid x:Name ="LayoutRoot" >
11 < StackPanel >
12 < TextBlock x:Name ="txt" ></ TextBlock >
13
14
15 < my:Label >
16 < my:Label.Content >
17 < Rectangle Fill ="Red" Width ="100" Height ="100" ></ Rectangle >
18 </ my:Label.Content >
19 </ my:Label > </ StackPanel >
20 </ Grid >
21 </ navigation:Page >
22

 

 

 

代码
   
     
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.Windows.Navigation;
13
14 namespace FramePageDemo
15 {
16 public partial class Page2 : Page
17 {
18 public Page2()
19 {
20 InitializeComponent();
21 }
22
23 // Executes when the user navigates to this page.
24 protected override void OnNavigatedTo(NavigationEventArgs e)
25 {
26 }
27
28 private void Page_Loaded( object sender, RoutedEventArgs e)
29 {
30 try
31 {
32 txt.Text = NavigationContext.QueryString[ " date " ];
33 }
34 catch
35 {
36
37
38 }
39 }
40
41 }
42 }
43

 

你可能感兴趣的:(silverlight)