除下面的方法外,还可以添加HyperlinkButton,设置NavigateUri属性来实现跳转。 WP7界面间的跳转相对于Android比较怪,跟HTML倒比较相似,就是生成一个Uri,这个Uri可以跟HTML中的URL一样带参数,然后跳转到这个URL就行。 效果: 只为演示页面的跳转,所以很简单,就一个按钮。 有两个Page,一个是建立项目时自动生成的MainPage,另一个是后来添加的Page1。WP7也采用XAML定义布局,文件扩展名为xaml,其实就本质上就是xml。 虽然布局很简单,但是因为是新手,还是贴一下: MainPage.xaml:
<phone:PhoneApplicationPage
x:Class="PhoneApp2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="MainPage" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Jump to page1" Height="73" HorizontalAlignment="Left" Margin="69,83,0,0" Name="button1" VerticalAlignment="Top" Width="277" Click="button1_Click" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
就一个按钮,定义了Click事件调用button1_Click方法 Page1.xaml:
<phone:PhoneApplicationPage
x:Class="PhoneApp2.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Page1" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Back to MainPage" Height="87" HorizontalAlignment="Left" Margin="26,211,0,0" Name="button1" VerticalAlignment="Top" Width="371" Click="button1_Click" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
同样一个按钮。 下面是关键部分了,看如何从MainPage跳到Page1: MainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//点击按钮跳转到Page1
NavigationService.Navigate(new Uri("/Page1.xaml?arg1=aaa&arg2=bbb", UriKind.Relative));
//写过HTML代码一定会觉得很熟悉,参数是一个Uri对象,后面的UriKind.Relative是表示这个是相对路径
}
}
}
再从Page1跳回到MainPage,以及接收来自MainPage传过来的参数: Page1.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
namespace PhoneApp2
{
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
}
//重写父类PhoneApplicationPage的 OnNavigatedTo方法,即从其他页面跳转到此时执行
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string arg1 = "";
string arg2 = "";
NavigationContext.QueryString.TryGetValue("arg2", out arg2);
NavigationContext.QueryString.TryGetValue("arg1", out arg1);
//也可以用NavigationContext.QueryString["args1"]来获取值,但上面的TryGetValue返回的是bool值,相当于try catch语句,可以处理异常,这里没有这样用
MessageBox.Show("传过来的参数是:arg1=" + arg1+" arg2="+arg2);
//弹个对话框显示传过来的参数
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//点击按钮返回到
//NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
NavigationService.GoBack();
//如果使用上面一行的Navigate方法,不会返回到前一页面,而是新创建一个MainPage的实例
//使用GoBack()之前可以用CanGoBack判断一下是否可返回
}
}
}
打包一下源代码: [download id=”22”]