WP7之页面导航

在Windows Phone的页面中,可以使用以下2种方式进行导航:

1.使用xmal进行导航

2.使用C#代码导航 

框架与页面架构

Silverlight for Windows Phone与传统的Silverlight类似,都有一个Frame,是一个单独的PhoneAppFrame,与传统的SL页面的区别是MS实现了PhoneApplicationFrame和PhoneApplicationPage 2个类,一个Frame包含系统托盘和应用程序栏,一个Frame里面可以包含一个名多个Page,一个Page包含标题,可以有独立的应用程序栏,Page之间可以相互Navigation(导航)。 

页面导航(Page Navigation)

Silverlight for Windows Phone使用以页面为基础的导航模型,Silverlight for Windows Phone的页面导航与web页面的导航模型类似,因为每个独立的页面都有唯一的URI,每个独立的页面都是没有状态的(相对于WinForm来说)

使用XAML进行导航,以HyperlinkButton为例 

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<!--使用Xaml导航-->
<HyperlinkButton Content="Music" NavigateUri="/Pages/Music.xaml" Height="30" HorizontalAlignment="Left" Margin="9,121,0,0" Name="hlnkbtnMusic" VerticalAlignment="Top" Width="200" />
<HyperlinkButton Content="Vedio" NavigateUri="/Pages/Vedio.xaml" Height="30" HorizontalAlignment="Left" Margin="13,228,0,0" Name="hlnkbtnVedio" VerticalAlignment="Top" Width="200" />
<HyperlinkButton Content="Picture" NavigateUri="/Pages/Picture.xaml" Height="30" HorizontalAlignment="Left" Margin="13,359,0,0" Name="hlnkbtnPic" VerticalAlignment="Top" Width="200" />
</Grid>

使用C#代码进行导航,以Button为例

Xaml页面代码 

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<!--使用Xaml导航-->
<Button Content="Music" Height="72" HorizontalAlignment="Left" Margin="246,100,0,0" Name="btnMusic" VerticalAlignment="Top" Width="160" Click="btnMusic_Click" />
<Button Content="Vedio" Height="72" HorizontalAlignment="Left" Margin="246,207,0,0" Name="btnVedio" VerticalAlignment="Top" Width="160" Click="btnVedio_Click" />
<Button Content="Picture" Height="72" HorizontalAlignment="Left" Margin="246,338,0,0" Name="btnPicture" VerticalAlignment="Top" Width="160" Click="btnPicture_Click" />
</Grid>

后台C#代码 

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 _4PageNavigation
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()

{
InitializeComponent();
}
//点击Music按钮
private void btnMusic_Click(object sender, RoutedEventArgs e)

{
//使用NavigationService的Navigate方法进行导航
NavigationService.Navigate(new Uri("/Pages/Music.xaml",UriKind.Relative));

}
//点击Vedio按钮
private void btnVedio_Click(object sender, RoutedEventArgs e)

{
NavigationService.Navigate(new Uri("/Pages/Vedio.xaml", UriKind.Relative));
}
//点击Picture按钮
private void btnPicture_Click(object sender, RoutedEventArgs e)

{
NavigationService.Navigate(new Uri("/Pages/Picture.xaml", UriKind.Relative));
}
}
}

别名地址导航

使用别名地址进行导航,首先要修改App.xaml文件,引入一个命名空间,然后进行地址别名映射,代码修改如下: 

<Application 
x:Class="_4PageNavigation.App"
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:nav
="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"><!--这里需要引入一个命名空间-->

<!--Application Resources-->
<Application.Resources>
<!--在这里时行地址别名映射-->
<nav:UriMapper x:Key="UriMapper">
<!--
别名:Music, 导航到的地址:/Pages/Music.xaml
别名:Vedio, 导航到的地址:/Pages/Vedio.xaml
别名:Picture,导航到的地址:/Pages/Picture.xaml
-->

<nav:UriMapping Uri="Music" MappedUri="/Pages/Music.xaml"/>
<nav:UriMapping Uri="Vedio" MappedUri="/Pages/Vedio.xaml"/>
<nav:UriMapping Uri="Picture" MappedUri="/Pages/Picture.xaml"/>
</nav:UriMapper>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated
="Application_Activated" Deactivated="Application_Deactivated"/>

</Application.ApplicationLifetimeObjects>
</Application>

然后修改App.xaml.cs文件里的构造函数,代码如下:

     public App()
{ // Global handler for uncaught exceptions.
UnhandledException += Application_UnhandledException;


// Standard Silverlight initialization
InitializeComponent();

// Phone-specific initialization
InitializePhoneApplication();
            this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper; //修改这里,映射到App.xaml当中的UriMapper,就是Key关键字当中指定的。

// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)

{
// Display the current frame rate counters.
Application.Current.Host.Settings.EnableFrameRateCounter = true;


// Show the areas of the app that are being redrawn in each frame.
//Application.Current.Host.Settings.EnableRedrawRegions = true;

// Enable non-production analysis visualization mode,
// which shows areas of a page that are handed off to GPU with a colored overlay.
//Application.Current.Host.Settings.EnableCacheVisualization = true;

// Disable the application idle detection by setting the UserIdleDetectionMode property of the
// application's PhoneApplicationService object to Disabled.
// Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
// and consume battery power when the user is not using the phone.
PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;

}
}

页面之间的数据传递

使用参数传递

传递数据——>传递对象

NavigationServices 默认不能传递对象

可用方法

使用App类的静态属性

使用Singleton类

把对象分解使用QueryString来传递 

回退:NavigaationServices.goback()

 

转载于:https://www.cnblogs.com/kbillows/archive/2012/01/11/2320024.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值