Windows Phone页面导航和独立存储开发总结

一、页面导航

windows phone页面导航可以通过两种方法进行设置:一种是直接在XAML中设置;另一种则需要通过编码在托管代码中实现。二者相同的地方是它们最终都需要使用NavigationService类,再调用Navigate方法实现导航。

1、在XAML中设置导航

最简单直接的方式,找到控件,为属性NavigateUri赋值即可,以常用的HyperlinkButton为例:

<HyperlinkButton Content="图片" Height="30" Name="lbtnPicture" Width="200" NavigateUri="/View/Pictures.xaml" />

其中,使用“/”意味着这是一个相对链接,Silverlight for Windows Phone目录层次的路径操作比较严格,如将前面的“/”去掉,编译器将报错并停止运行。

 

2、托管代码中设置导航

(1)、直接传入URI

        void lbtnPicture_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/Pictures.xaml", UriKind.Relative));
        }

(2)、使用地址别名

地址别名是web技术中的一项很好的扩展,windows phone开发中同样适用。可以将较长命名的URI缩短为一段较短且好记的URI来呈现,比如将/View/Pictures.xaml缩短为Pic:

   void lbtnPicture_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("Pic", UriKind.Relative));
        }

看到“Pic”大家会想到它一定映射到具体的某个URI(“/View/Pictures.xaml”)。如何进行映射呢?

需要在App.xaml.cs文件中额外引入UriMapper别名,映射所在的命名空间如下所示:

 xmlns:navigation="clr-namespace:System.Windows.Navigation;assembly=Microsoft.Phone"

 

实现地址别名,是基于Microsoft.Phone链接库的,接着就是配置对应的映射关系:

 <navigation:UriMapper x:Key="UriMap">
            <navigation:UriMapping Uri="Pic" MappedUri="/View/Pictures.xaml" />
        </navigation:UriMapper> 

接着,我们要在App.xaml.cs的构造函数中获取地址别名,将地址别名的key提取到UriMapper对象中:

UriMap        /// <summary>
        /// Constructor for the Application object.
        /// </summary>
        public App()
        {
            // Global handler for uncaught exceptions. 
            UnhandledException += Application_UnhandledException;

            // 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 being GPU accelerated with a colored overlay.
                //Application.Current.Host.Settings.EnableCacheVisualization = true;
            }

            // Standard Silverlight initialization
            InitializeComponent();

            // Phone-specific initialization
            InitializePhoneApplication();
            this.RootFrame.UriMapper = Resources["UriMap"] as UriMapper; //将地址别名的key提取到UriMapper对象中
        }

 

3、页面间传参

有过web开发经验的应该很清楚web开发中几种常见的页面参数传递方式。Silverlight for Windows Phone导航参数的传递非常类似于web开发中的get方式,也就是QueryString的方式。

下面以一个WebBrowser页面来讲解下页面间QueryString的传参方式:

        //导航到浏览器页面
        void btnWebBrowser_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/WebBrowser.xaml?uri=http://www.cnblogs.com", UriKind.Relative));
        }

如果觉得参数太长,可以使用地址别名的方式:

    //导航到浏览器页面
        void btnWebBrowser_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("Web/http://www.cnblogs.com", UriKind.Relative));
        }

相应的,App.xaml文件中UriMapper代码做出适当修改:

    <!--Application Resources-->
    <Application.Resources>
        
        <navigation:UriMapper x:Key="UriMap">
            <navigation:UriMapping Uri="Web/{weburi}" MappedUri="/View/WebBrowser.xaml?uri={weburi}" />
        </navigation:UriMapper>   
        
    </Application.Resources>

其中,{weburi}变量称为参数变量,它是代替真实地址别名化后传递的参数值。

我们接着在WebBrowser.xaml页面拖拽一个WebBrowser控件和一个提示文本框控件,用于显示,xaml如下:

WebBrowser    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <phone:WebBrowser HorizontalAlignment="Left" Name="wpBrowser" VerticalAlignment="Top" Height="529" Width="452" Margin="4,6,0,0" />
            <TextBlock Height="60" HorizontalAlignment="Left" Margin="12,541,0,0" Name="txtMsg" Text="TextBlock" VerticalAlignment="Top" Width="438" />
        </Grid>
    </Grid>

 

接着在后台代码中编写:

QueryStringusing System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace NavigationService_ch.View
{
    public partial class WebBrowser : PhoneApplicationPage
    {
        public WebBrowser()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(WebBrowser_Loaded);
        }

        void WebBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            var dict = NavigationContext.QueryString; //页面导航参数字典
            if (dict == null || dict.Count == 0)
            {
                return;
            }

            wpBrowser.Navigate(new Uri(dict["uri"], UriKind.RelativeOrAbsolute));
            this.txtMsg.Text = "正在打开:" + dict["uri"] + "...";
            wpBrowser.Navigated += new EventHandler<NavigationEventArgs>(wpBrowser_Navigated); //导航完成后事件
        }


        void wpBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            this.txtMsg.Text = "页面打开已完成";
        }

    }
}

对于页面导航的生命周期,可通过以下事件进行跟踪:Navigating、Navigated、NavigationProgress、NavigationFailed、NavigationStopped、LoadCompleted、FragmentNavigation 。具体情况大家可以参考MSDN的讲解。
这种通过WebBrowser控件导航到具体页面的方式实际开发中会经常用到。如果需要返回到上一页,硬件设备上提供了回退按钮,实际上它是基于NavigationService的GoBack方法实现的:

        void btnGoBack(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

我们可以通过BackKeyPress事件编程禁用回退功能:

CancelGoBack         public WebBrowser()
        {
            InitializeComponent();
            //注消页面的回退事件
            this.BackKeyPress += new EventHandler<CancelEventArgs>(WebBrowser_BackKeyPress);
        }

        void WebBrowser_BackKeyPress(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

最后再来总结下QueryString传参的几个特点:

a、使用“?”号区分传递参数与导航URI;

b、传递的参数之间使用“&”进行连接;

c、参数的值都只能是字符串。

QueryString传参的缺点是对于某些对象而不仅仅是字符串的传参比较难以处理。虽然我们可以通过序列化对象为字符串进行参数传递,但是序列化和反序列消耗CPU资源,对性能要求较高的移动设备,这样的资源消耗必须是越少越好。

个人认为windows phone导航技术(包括传参)完全可以和web中的导航技术类比,两者非常相似。

 

二、独立存储

所谓独立存储,字面理解即应用程序与应用程序之间存储空间相互隔离,不会相互影响。有过web开发的应该记得web开发中通用的cookie技术,通过cookie可以在客户端保存一些用户信息,并且理论上是相互独立的,每一个会话都对应一个cookie。IsolatedStorage处理机制类似于cookie,不同之处在于IsolatedStorage可以存储任何类型的资源,而cookie只能存储文本。

System.IO.IsolatedStorage 命名空间中几个重要类包括:

IsolatedStorageFile:表示包含文件和目录的独立存储区;

IsolatedStorageFileStream :公开独立存储中的文件;

IsolatedStorageSettings:提供一个在独立存储中存储键/值对的 Dictionary<TKey, TValue>;

IsolatedStorageException:独立存储中的操作失败时所引发的异常。

1、目录操作

windows phone的目录操作和.NET中常见的几种目录操作类似,比如判断目录是否存在、创建和删除目录等等:

DirectoryOperationsusing System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;

namespace SimpleWinPhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static readonly string dir = "SimpleWinPhoneApp";

        public MainPage()
        {
            InitializeComponent();
            btnDirExists.Click += new RoutedEventHandler(DirectoryExistsButton_Click);
            btnDirCreate.Click += new RoutedEventHandler(CreateDirectoryButton_Click);
            btnDirDelete.Click += new RoutedEventHandler(DeleteDirectoryButton_Click);
        }

        /// <summary>
        /// 是否存在目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DirectoryExistsButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.DirectoryExists(dir)) //是否存在
                    {
                        MessageBox.Show("目录存在");
                    }
                    else
                    {
                        MessageBox.Show("目录不存在");
                    }
                }

            }
            catch (IsolatedStorageException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DeleteDirectoryButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    file.DeleteDirectory(dir);
                }
            }
            catch (IsolatedStorageException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CreateDirectoryButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (file.DirectoryExists(dir))
                    {
                        MessageBox.Show("目录已存在");
                        return;
                    }
                    file.CreateDirectory(dir);
                }
            }
            catch (IsolatedStorageException ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}

 

需要注意的是,Silverlight for Windows Phone独立存储空间是针对当前用户而言的,空间内的权限已经赋给了当前APP的使用用户,这些权限都是最高级别的,这样就不会产生操作权限不足的问题。

2、文件操作

windows phone文件的操作需要遵循以下步骤:

a、获取应用程序的独立存储空间,可通过IsolatedStorageFile文件操作基类的GetUserStoreForApplication方法进行获取;

b、利用返回的对象,使用该对象提供的方法进行文件操作;

c、文件流操作(比如读取或者写入)结束时,需将该文件关闭,再放回独立存储空间;

d、文件操作异常,可以通过IsolatedStorageException进行异常捕获。

windows phone中常见的文件类型包括简单文本、图片、多媒体等。其中文本文件操作非常简单,比如创建、删除、读取和写入等等,而图片和多媒体稍微复杂一些,但是都有对应的API可供操作,涉及图片和多媒体文件的处理内容较为丰富,本文暂不详细贴出API如何调用。

3、应用程序配置文件

应用程序配置文件主要通过IsolatedStorageSettings类进行操作。 IsolatedStorageSettings提供了一个泛型字典类Dictionary<TKey, TValue>,通过键值Tkey 与数值TValue 的映射将应用程序的数据存储起来。对比其他类型的.net应用程序配置文件的操作,有很多类似的地方:

ConfigFileOperations

转载于:https://www.cnblogs.com/xiaogui9527/archive/2013/03/27/2983920.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值