• 页面导航【WP7学习札记之七】


           本节是WP7学习札记的第七篇,讲述的内容摘要主要是将页面导航的两种方式、地址别名、页面之间的数据传递(包括传递字符串、和传递对象两种方式)、回退按钮(重写Back键的事件),具体如下:

          首先讲述下Windows Phone 7应用程序的页面架构,只有一个单独的PhoneApplicationFrame,包含一个或者多个PhoneApplicationPage,也包含系统托盘和应用程序栏。

          接着是页面导航的两种方式,分别是xaml方式和c#方式。需要注意的是Silverlight for Windows Phone使用以页面为基础的导航模型,与Web的页面导航模型相似,每个页面都有唯一的URI,每个页面都是没有状态的。

          首先是使用xaml进行导航(NavigateUri=“/Views/Music.xaml”)

    <HyperlinkButton  NavigateUri="/Views/Music.xaml" Content="音乐" Height="30" HorizontalAlignment="Left" Margin="23,74,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="200" />

          接着是c#代码的方式进行导航(NavigationService.Navigate(new Uri("/Views/Music.xaml",UriKind.Relative))):

     private void button1_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.Navigate(new Uri("/Views/Music.xaml", UriKind.Relative));
    }

     下面是别名地址导航,这个要详细说下。具体的做法是:

        在App.xaml文件的xaml文件中,引入xmlns:nav="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone" 这个xaml名称空间;在<resources>中定义如下的格式:

    <!--Application Resources-->
    <Application.Resources>
    <nav:UriMapper x:Key="UriMapper">
    <nav:UriMapping Uri="Music" MappedUri="/Views/Music.xaml"/>
    <nav:UriMapping Uri="Music/{music}" MappedUri="/Views/Music.xaml?Music={music}"/>
    </nav:UriMapper>
    </Application.Resources>

       在App.xaml.cs中的App构造函数Public App(){}中插入如下的代码:

    this.RootFrame.UriMapper = Resources["UriMapper"] as UriMapper;

       接着在需要使用别名导航的地方使用如下的方式:

    <HyperlinkButton Content="音乐"  NavigateUri="Music" Height="30" HorizontalAlignment="Left" Margin="23,189,0,0" Name="hyperlinkButton2" VerticalAlignment="Top" Width="200" />

    OK,搞定~

         下面讲的是页面间传递数据--字符串数据。

    上面这个图不是很全面,我做点补充。首先在原页面写类似下面的代码(分别是使用了别名导航和不使用别名导航的两种情况):

    <HyperlinkButton NavigateUri="/Views/Music.xaml?Music=歌曲1" Content="歌曲1" Height="30" HorizontalAlignment="Left" Margin="49,284,0,0" Name="hyperlinkButton3" VerticalAlignment="Top" Width="131" />
    <HyperlinkButton NavigateUri="Music/歌曲1" Content="歌曲1" Height="30" HorizontalAlignment="Left" Margin="231,284,0,0" Name="hyperlinkButton4" VerticalAlignment="Top" Width="131" />

    然后在“导航到的页面”PageLoad事件中写如下代码(和Web开发中神似~):

     private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
    if(NavigationContext.QueryString.Count>0)
    {
    this.textBlock1.Text = NavigationContext.QueryString["Music"];
    }
    }

    OK,传递字符串完毕~

          在页面间传递对象数据,NavigationService默认不能传递对象。可用的方法有:①使用App类的静态属性;②使用Singleton类;③把对象分解使用Query string来传递;

    我比较倾向于使用第一种方法:

        解释一下:首先在项目下添加一个类;

        public class MusicClass
    {
    public string Name { get; set; }
    public string File { get; set; }
    }

        在App类中添加如下属性:

    public static MusicClass Music { get; set; }//添加这个属性

        在传递的原页面添加如下代码:

            private void button2_Click(object sender, RoutedEventArgs e)
    {
    App.Music = new MusicClass()
    {
    Name="歌曲1", File="music1.mp3"
    };
    NavigationService.Navigate(new Uri("/Views/Music.xaml", UriKind.Relative));
    }

        在目标页面,代码类似如下:

            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
    if (App.Music != null)
    {
    this.textBlock1.Text = App.Music.Name;
    }
    }

    Ok,解释完毕~

        程序可以提供控件实现回退功能,硬件Back按钮也能回退到前一个也页面(不需要编码,内置实现~)

    但是,如果回退到前一个页面是不合理的行为,可以重写“Back 按钮”的时间处理,终止回退。

            private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
    e.Cancel = true;
    }

    注意,仅是Back键~
    希望对各位博友有帮助~

  • 相关阅读:
    C#规范整理·异常与自定义异常
    C#规范整理·资源管理和序列化
    C#规范整理·泛型委托事件
    C#规范整理·集合和Linq
    <抽象工厂>比<工厂方法>多了啥(区别)
    <工厂方法>比<简单工厂>多了啥(区别)
    Unable to start Ocelot because either a ReRoute or GlobalConfiguration
    MySQL服务安装
    mysql登录报错“Access denied for user 'root'@'localhost' (using password: YES”)的处理方法
    使用博客系统发生_STORAGE_WRITE_ERROR_错误
  • 原文地址:https://www.cnblogs.com/DebugLZQ/p/2384062.html
Copyright © 2020-2023  润新知