原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密

[索引页]
[源码下载]

与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之特性

  • 手机方向
  • 本地化
  • 应用程序的试用体验
  • 系统主题资源
  • 本地数据的加密解密

示例
1、演示如何响应手机的方向变化
Orientation.xaml

  1. <phone:PhoneApplicationPage
  2. x:Class="Demo.Feature.Orientation"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <Grid x:Name="LayoutRoot" Background="Transparent">
  17.  
  18. <TextBlock Text="改变手机方向,以查看演示效果" />
  19.  
  20. </Grid>
  21.  
  22. </phone:PhoneApplicationPage>

Orientation.xaml.cs

  1. /*
  2. * 演示如何捕获手机方向改变的事件
  3. *
  4. * PhoneApplicationPage - 页面
  5. * SupportedOrientations - 支持的方向(Microsoft.Phone.Controls.SupportedPageOrientation 枚举)
  6. * Portrait(竖), Landscape(横), PortraitOrLandscape(横竖)
  7. * Orientation - 当前的方向(Microsoft.Phone.Controls.PageOrientation 枚举)
  8. * None, Portrait, Landscape, PortraitUp, PortraitDown(目前不支持), LandscapeLeft(PortraitUp 逆时针转到 Landscape), LandscapeRight(PortraitUp 顺时针转到 Landscape)
  9. * OrientationChanged - 方向改变后触发的事件(事件参数:OrientationChangedEventArgs)
  10. *
  11. * OrientationChangedEventArgs
  12. * Orientation - 当前的页面方向
  13. */
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Animation;
  25. using System.Windows.Shapes;
  26. using Microsoft.Phone.Controls;
  27.  
  28. namespace Demo.Feature
  29. {
  30. public partial class Orientation : PhoneApplicationPage
  31. {
  32. public Orientation()
  33. {
  34. InitializeComponent();
  35.  
  36. this.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
  37. this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(Orientation_OrientationChanged);
  38. }
  39.  
  40. void Orientation_OrientationChanged(object sender, OrientationChangedEventArgs e)
  41. {
  42. if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
  43. {
  44. MessageBox.Show("竖放");
  45. }
  46. else
  47. {
  48. MessageBox.Show("横放");
  49. }
  50. }
  51. }
  52. }

2、演示如何实现程序的本地化
Localization.xaml

  1. <phone:PhoneApplicationPage
  2. x:Class="Demo.Feature.Localization"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <phone:PhoneApplicationPage.Resources>
  17. <!--
  18. 注:AppResources 里的类和构造函数需要 public 的要手动改成 public
  19. -->
  20. <resources:AppResources xmlns:resources="clr-namespace:Demo.Resources" x:Key="MyLocalization" />
  21. </phone:PhoneApplicationPage.Resources>
  22.  
  23. <Grid x:Name="LayoutRoot" Background="Transparent">
  24. <StackPanel Orientation="Vertical">
  25.  
  26. <TextBlock Name="lblMsg" />
  27.  
  28. <!--
  29. xaml 方式应用本地化
  30. -->
  31. <TextBlock Text="{Binding Path=Software, Source={StaticResource MyLocalization}}" />
  32.  
  33. </StackPanel>
  34. </Grid>
  35.  
  36. </phone:PhoneApplicationPage>

Localization.xaml.cs

  1. /*
  2. * 演示如何本地化应用程序,相关资源文件在 Resources 目录下
  3. *
  4. * 注:本地化应用程序标题(标题分为两种:应用程序列表中的标题和“磁贴”上的应用程序标题,它们可以不同)请参看 http://msdn.microsoft.com/en-us/library/ff967550(v=vs.92).aspx
  5. */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Animation;
  17. using System.Windows.Shapes;
  18. using Microsoft.Phone.Controls;
  19.  
  20. using Demo.Resources;
  21.  
  22. namespace Demo.Feature
  23. {
  24. public partial class Localization : PhoneApplicationPage
  25. {
  26. public Localization()
  27. {
  28. InitializeComponent();
  29.  
  30. // 代码方式应用本地化
  31. lblMsg.Text = AppResources.Network;
  32. }
  33. }
  34. }

3、演示如何提供支持试用体验的应用程序
TrialExperience.xaml

  1. <phone:PhoneApplicationPage
  2. x:Class="Demo.Feature.TrialExperience"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <Grid x:Name="LayoutRoot" Background="Transparent">
  17. <StackPanel Orientation="Vertical">
  18.  
  19. <Button Name="btnTrial" Content="试用版可用的功能" Click="btnTrial_Click" />
  20.  
  21. <Button Name="btnNormal" Content="正式版里才有的功能" Click="btnNormal_Click" />
  22.  
  23. <Button Name="btnBuy" Content="购买正式版" Click="btnBuy_Click" />
  24.  
  25. </StackPanel>
  26. </Grid>
  27.  
  28. </phone:PhoneApplicationPage>

TrialExperience.xaml.cs

  1. /*
  2. * 演示如何在收费应用程序中加入试用体验
  3. *
  4. * LicenseInformation - 应用程序的许可证信息
  5. * IsTrial() - 是否是试用版
  6. */
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Animation;
  18. using System.Windows.Shapes;
  19. using Microsoft.Phone.Controls;
  20.  
  21. using Microsoft.Phone.Marketplace;
  22. using Microsoft.Phone.Tasks;
  23.  
  24. namespace Demo.Feature
  25. {
  26. public partial class TrialExperience : PhoneApplicationPage
  27. {
  28. private static LicenseInformation _licenseInfo = new LicenseInformation();
  29.  
  30. private static bool _isTrial = true;
  31.  
  32. public TrialExperience()
  33. {
  34. InitializeComponent();
  35.  
  36. // 检查许可证,仅为测试用,实际项目中最好将此逻辑放到 Application_Launching 和 Application_Activated 中
  37. CheckLicense();
  38. }
  39.  
  40. private void CheckLicense()
  41. {
  42. #if DEBUG
  43. // 仅调试用,强行指定程序为试用版
  44. _isTrial = true;
  45. #else
  46. // 获取当前安装到设备的版本是否为试用版
  47. _isTrial = _licenseInfo.IsTrial();
  48. #endif
  49.  
  50. // 如果是试用版则禁用正式版的功能
  51. if (_isTrial)
  52. btnNormal.IsEnabled = false;
  53. }
  54.  
  55. private void btnTrial_Click(object sender, RoutedEventArgs e)
  56. {
  57. MessageBox.Show("试用版可用的功能");
  58. }
  59.  
  60. private void btnNormal_Click(object sender, RoutedEventArgs e)
  61. {
  62. MessageBox.Show("正式版里才有的功能");
  63. }
  64.  
  65. private void btnBuy_Click(object sender, RoutedEventArgs e)
  66. {
  67. // 购买的话,直接打开应用程序在商店的详细页就好
  68. MarketplaceDetailTask marketPlaceDetailTask = new MarketplaceDetailTask();
  69. marketPlaceDetailTask.Show();
  70. }
  71. }
  72. }

4、演示如何使用系统主题资源
ThemeResources.xaml

  1. <phone:PhoneApplicationPage
  2. x:Class="Demo.Feature.ThemeResources"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <Grid x:Name="LayoutRoot" Background="Transparent">
  17.  
  18. <TextBlock Text="使用系统主题资源" Style="{StaticResource PhoneTextExtraLargeStyle}" />
  19.  
  20. <HyperlinkButton Content="全部系统主题资源" NavigateUri="http://msdn.microsoft.com/en-us/library/ff769552(v=vs.92)" TargetName="_blank" />
  21.  
  22. </Grid>
  23.  
  24. </phone:PhoneApplicationPage>

5、演示如何实现本地数据的加密解密
EncryptData.xaml

  1. <phone:PhoneApplicationPage
  2. x:Class="Demo.Feature.EncryptData"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  10. FontSize="{StaticResource PhoneFontSizeNormal}"
  11. Foreground="{StaticResource PhoneForegroundBrush}"
  12. SupportedOrientations="Portrait" Orientation="Portrait"
  13. mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  14. shell:SystemTray.IsVisible="True">
  15.  
  16. <Grid x:Name="LayoutRoot" Background="Transparent">
  17.  
  18. <TextBlock Text="注:不同的应用程序有不同的密钥" />
  19.  
  20. </Grid>
  21.  
  22. </phone:PhoneApplicationPage>

EncryptData.xaml.cs

  1. /*
  2. * 演示如何对数据做加密解密
  3. *
  4. * ProtectedData - 用于加密解密数据
  5. * Protect() - 加密数据
  6. * Unprotect() - 解密数据
  7. *
  8. * 注:不同的应用程序有不同的密钥,所以本示例的加密解密仅针对同一应用程序有效。应用场景为:本地数据的加密解密
  9. */
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Animation;
  21. using System.Windows.Shapes;
  22. using Microsoft.Phone.Controls;
  23.  
  24. using System.Security.Cryptography;
  25. using System.Text;
  26.  
  27. namespace Demo.Feature
  28. {
  29. public partial class EncryptData : PhoneApplicationPage
  30. {
  31. private string _base64 = "";
  32.  
  33. public EncryptData()
  34. {
  35. InitializeComponent();
  36.  
  37. EncryptDemo();
  38.  
  39. DecryptDemo();
  40. }
  41.  
  42. private void EncryptDemo()
  43. {
  44. byte[] plainText = Encoding.UTF8.GetBytes("webabcd");
  45. byte[] protectedData = ProtectedData.Protect(plainText, null);
  46.  
  47. _base64 = Convert.ToBase64String(protectedData);
  48.  
  49. MessageBox.Show("字符串“webabcd”加密且base64后的数据:" + Environment.NewLine + _base64);
  50. }
  51.  
  52. private void DecryptDemo()
  53. {
  54. byte[] protectedData = Convert.FromBase64String(_base64);
  55. byte[] plainText = ProtectedData.Unprotect(protectedData, null);
  56.  
  57. MessageBox.Show("解密后的数据:" + Environment.NewLine + Encoding.UTF8.GetString(plainText, , plainText.Length));
  58. }
  59. }
  60. }

OK
[源码下载]

与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密的更多相关文章

  1. 与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏

    原文:与众不同 windows phone (27) - Feature(特性)之搜索的可扩展性, 程序的生命周期和页面的生命周期, 页面导航, 系统状态栏 [索引页][源码下载] 与众不同 wind ...

  2. 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法

    原文:重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法 [源码下载] 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈 ...

  3. 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复

    [源码下载] 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 作者:webabcd 介绍与众不同 win ...

  4. 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展

    [源码下载] 与众不同 windows phone (43) - 8.0 相机和照片: 镜头的可扩展性, 图片的可扩展性, 图片的自动上传扩展 作者:webabcd 介绍与众不同 windows ph ...

  5. 与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器

    原文:与众不同 windows phone (18) - Device(设备)之加速度传感器, 数字罗盘传感器 [索引页][源码下载] 与众不同 windows phone (18) - Device ...

  6. 与众不同 windows phone (13) - Background Task(后台任务)之后台文件传输(上传和下载)

    原文:与众不同 windows phone (13) - Background Task(后台任务)之后台文件传输(上传和下载) [索引页][源码下载] 与众不同 windows phone (13) ...

  7. 与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任务)

    原文:与众不同 windows phone (12) - Background Task(后台任务)之 PeriodicTask(周期任务)和 ResourceIntensiveTask(资源密集型任 ...

  8. 与众不同 windows phone (4) - Launcher(启动器)

    原文:与众不同 windows phone (4) - Launcher(启动器) [索引页][源码下载] 与众不同 windows phone (4) - Launcher(启动器) 作者:weba ...

  9. 与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦

    原文:与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦 [索引页][源码下载] ...

随机推荐

  1. zoj3713 7Bit

    意思是把一行字符串的长度按照找7位一个字节输出,如果长度能够存在7位里,字节的最高位置0,否则只输出7位并且输出字节的最高位置1,直到全部输出长度. 要注意的是有空串要输出00,其他按照16进制输出就 ...

  2. Integral Promotions整数提升和符号扩展(char,unsigned char,signed char)

    以下来自msdn: Objects of an integral type can be converted to another wider integral type (that is, a ty ...

  3. 承诺消费换4G无线上网伴侣活动火热来袭,各指定营业厅即可办理

    承诺消费换4G无线上网伴侣活动火热来袭,各指定营业厅即可办理 承诺消费换4G无线上网伴侣活动火热来袭,各指定营业厅即可办理

  4. [置顶] Android中使用sqlite3操作SQLite

    SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.创建数据库:  1.将sqlit ...

  5. API通用设计原则

    什么是好的API? ·        完备(Be Complete) 对确定重点支持的用户场景具有完备的功能支持.就是说,用户通过对一组API的调用能够完成预期的功能. ·        不冗余(Be ...

  6. ORA-01403: no data found

    在项目的存储过程中有这样一句话 select jgdm,jgmc into parm_mrjgdm,parm_mrjgmc From BL_KHXX  where jgdm=PARM_JGDM; 每次 ...

  7. LoadRunner 监控 Apache

    配置Apache部分 1.修改Apache中Httpd.conf文件 <Location /server-status> SetHandler server-status Order de ...

  8. c# 未能载入文件或程序集

    近期做项目时碰到这个问题了.goole.百度了半天,整理了下面几种可能: DLL文件名称与载入时的DLL文件名称不一致, DLL文件根本不存在,即出现丢失情况, 载入DLL路径错误,即DLL文件存在, ...

  9. QTableWidget表格合并若干问题及解决方法

    Qt提供 QTableWidget作为表格的类以实现表格的基本功能,表格中所装载的每一个单元格由类QTableWidgetItem提供.这是基于表格实现 Qt提供的一个基础类,若想实现定制表格和单元格 ...

  10. Spring3 MVC 拦截器拦截不到的问题

    拦截器: com.zk.interceptors.MyInterceptor 实现了 HandlerInterceptor接口,可以拦截@RequestMapping注解的类和方法 第一种方式 < ...