[置顶] Silverlight之页面跳转及参数传递(9)

时间:2022-04-12 23:20:55

页面跳转及参数传递

代码下载

一、            页面跳转

1.        方式1 RootVisual

this.RootVisual = new MainPage();

2.        方式2 Content属性

  Page1 p1 =newPage1();

  this.Content=p1;

3.        方式3 FrameHyperlinkButton

1)定义HyperlinkButton

          <HyperlinkButton Content="导航" Height="23" HorizontalAlignment="Left" Margin="28,12,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="100" NavigateUri="/page1.xaml" TargetName="frame1" />

 2)定义框架

        <sdk:Frame Height="190" HorizontalAlignment="Left" Margin="12,75,0,0" Name="frame1" VerticalAlignment="Top" Width="350" Source="/page2.xaml"/>

 

后台:

this.frame1.Navigate(newUri("/SilverlightControl2.xaml",UriKind.Relative));

 

4.        方式4 Border

 <Border BorderBrush="Silver" BorderThickness="1" Height="100" Name="border1" Width="200"></Border>

 

  Page2 p2=newPage2();

  border1.Child = p2;

 

5.        方式5 uriMapper

1)定义uriMapper

  <navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}"

                              Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">

                <navigation:Frame.UriMapper>

                  <uriMapper:UriMapper>

                    <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>

                    <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>

                  </uriMapper:UriMapper>

                </navigation:Frame.UriMapper>

            </navigation:Frame>

2)应用

  <Border x:Name="LinksBorder" Style="{StaticResource LinksBorderStyle}">

                <StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}">

 

                    <HyperlinkButton x:Name="Link1" Style="{StaticResource LinkStyle}"

                                     NavigateUri="/Home" TargetName="ContentFrame"Content="主页"/>

                                     

                    <Rectangle x:Name="Divider1" Style="{StaticResource DividerStyle}"/>

                   

                    <HyperlinkButton x:Name="Link2" Style="{StaticResource LinkStyle}"

                                     NavigateUri="/About"TargetName="ContentFrame" Content="关于"/>

                    <Rectangle x:Name="Divider2" Style="{StaticResource DividerStyle}"/>

                    <HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}"

                                     NavigateUri="/PageTest"TargetName="ContentFrame" Content="测试"/>

                </StackPanel>

            </Border>

二、            页面间参数传递

Ø  普通参数传递方式

1)传递参数

    string uriText = String.Format("/page3.xaml?UserID={0}&Role={1}","刘备","董事长");

    this.frame1.Navigate(newUri(uriText, UriKind.Relative));

 

2)接收参数

    this.lblUserID.Text=  this.NavigationContext.QueryString["UserID"].ToString();

    this.lblRole.Text= this.NavigationContext.QueryString["Role"].ToString();

 

Ø  利用UriMapper

  <uriMapper:UriMapper x:Name="PageMapper">

 

            <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>

            <uriMapper:UriMapping Uri="/About/{name}/{url}" MappedUri="/Views/About.xaml?name={name}&amp;url={url}"/>

            <uriMapper:UriMapping Uri="/About/{parameter}" MappedUri="/Views/About.xaml?parameter={parameter}"/>

 

            <uriMapper:UriMapping Uri="/PageTest/{name}/{url}" MappedUri="/Views/PageTest.xaml?UserName={name}&amp;url={url}"/>

            <uriMapper:UriMapping Uri="/PageTest/{parameter}" MappedUri="/Views/PageTest.xaml?parameter={parameter}"/>

 

            <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>

        </uriMapper:UriMapper>

1)传递参数:

<HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}"

                                     NavigateUri="/PageTest/传智播客/it.cast.net" TargetName="ContentFrame" Content="测试"/>

 

2)接收参数:

  txtUserID.Text = "名字:" +this.NavigationContext.QueryString["UserName"] +"网址:" +this.NavigationContext.QueryString["url"];

代码下载