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

[索引页]
[源码下载]

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

作者:webabcd

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

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

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

<phone:PhoneApplicationPage
x:Class="Demo.Feature.Orientation"
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"> <Grid x:Name="LayoutRoot" Background="Transparent"> <TextBlock Text="改变手机方向,以查看演示效果" /> </Grid> </phone:PhoneApplicationPage>

Orientation.xaml.cs

/*
* 演示如何捕获手机方向改变的事件
*
* PhoneApplicationPage - 页面
* SupportedOrientations - 支持的方向(Microsoft.Phone.Controls.SupportedPageOrientation 枚举)
* Portrait(竖), Landscape(横), PortraitOrLandscape(横竖)
* Orientation - 当前的方向(Microsoft.Phone.Controls.PageOrientation 枚举)
* None, Portrait, Landscape, PortraitUp, PortraitDown(目前不支持), LandscapeLeft(PortraitUp 逆时针转到 Landscape), LandscapeRight(PortraitUp 顺时针转到 Landscape)
* OrientationChanged - 方向改变后触发的事件(事件参数:OrientationChangedEventArgs)
*
* OrientationChangedEventArgs
* Orientation - 当前的页面方向
*/ 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 Demo.Feature
{
public partial class Orientation : PhoneApplicationPage
{
public Orientation()
{
InitializeComponent(); this.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(Orientation_OrientationChanged);
} void Orientation_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
{
MessageBox.Show("竖放");
}
else
{
MessageBox.Show("横放");
}
}
}
}

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

<phone:PhoneApplicationPage
x:Class="Demo.Feature.Localization"
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"> <phone:PhoneApplicationPage.Resources>
<!--
注:AppResources 里的类和构造函数需要 public 的要手动改成 public
-->
<resources:AppResources xmlns:resources="clr-namespace:Demo.Resources" x:Key="MyLocalization" />
</phone:PhoneApplicationPage.Resources> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <TextBlock Name="lblMsg" /> <!--
xaml 方式应用本地化
-->
<TextBlock Text="{Binding Path=Software, Source={StaticResource MyLocalization}}" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

Localization.xaml.cs

/*
* 演示如何本地化应用程序,相关资源文件在 Resources 目录下
*
* 注:本地化应用程序标题(标题分为两种:应用程序列表中的标题和“磁贴”上的应用程序标题,它们可以不同)请参看 http://msdn.microsoft.com/en-us/library/ff967550(v=vs.92).aspx
*/ 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 Demo.Resources; namespace Demo.Feature
{
public partial class Localization : PhoneApplicationPage
{
public Localization()
{
InitializeComponent(); // 代码方式应用本地化
lblMsg.Text = AppResources.Network;
}
}
}

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

<phone:PhoneApplicationPage
x:Class="Demo.Feature.TrialExperience"
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"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <Button Name="btnTrial" Content="试用版可用的功能" Click="btnTrial_Click" /> <Button Name="btnNormal" Content="正式版里才有的功能" Click="btnNormal_Click" /> <Button Name="btnBuy" Content="购买正式版" Click="btnBuy_Click" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>

TrialExperience.xaml.cs

/*
* 演示如何在收费应用程序中加入试用体验
*
* LicenseInformation - 应用程序的许可证信息
* IsTrial() - 是否是试用版
*/ 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 Microsoft.Phone.Marketplace;
using Microsoft.Phone.Tasks; namespace Demo.Feature
{
public partial class TrialExperience : PhoneApplicationPage
{
private static LicenseInformation _licenseInfo = new LicenseInformation(); private static bool _isTrial = true; public TrialExperience()
{
InitializeComponent(); // 检查许可证,仅为测试用,实际项目中最好将此逻辑放到 Application_Launching 和 Application_Activated 中
CheckLicense();
} private void CheckLicense()
{
#if DEBUG
// 仅调试用,强行指定程序为试用版
_isTrial = true;
#else
// 获取当前安装到设备的版本是否为试用版
_isTrial = _licenseInfo.IsTrial();
#endif // 如果是试用版则禁用正式版的功能
if (_isTrial)
btnNormal.IsEnabled = false;
} private void btnTrial_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("试用版可用的功能");
} private void btnNormal_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("正式版里才有的功能");
} private void btnBuy_Click(object sender, RoutedEventArgs e)
{
// 购买的话,直接打开应用程序在商店的详细页就好
MarketplaceDetailTask marketPlaceDetailTask = new MarketplaceDetailTask();
marketPlaceDetailTask.Show();
}
}
}

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

<phone:PhoneApplicationPage
x:Class="Demo.Feature.ThemeResources"
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"> <Grid x:Name="LayoutRoot" Background="Transparent"> <TextBlock Text="使用系统主题资源" Style="{StaticResource PhoneTextExtraLargeStyle}" /> <HyperlinkButton Content="全部系统主题资源" NavigateUri="http://msdn.microsoft.com/en-us/library/ff769552(v=vs.92)" TargetName="_blank" /> </Grid> </phone:PhoneApplicationPage>

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

<phone:PhoneApplicationPage
x:Class="Demo.Feature.EncryptData"
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"> <Grid x:Name="LayoutRoot" Background="Transparent"> <TextBlock Text="注:不同的应用程序有不同的密钥" /> </Grid> </phone:PhoneApplicationPage>

EncryptData.xaml.cs

/*
* 演示如何对数据做加密解密
*
* ProtectedData - 用于加密解密数据
* Protect() - 加密数据
* Unprotect() - 解密数据
*
* 注:不同的应用程序有不同的密钥,所以本示例的加密解密仅针对同一应用程序有效。应用场景为:本地数据的加密解密
*/ 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.Security.Cryptography;
using System.Text; namespace Demo.Feature
{
public partial class EncryptData : PhoneApplicationPage
{
private string _base64 = ""; public EncryptData()
{
InitializeComponent(); EncryptDemo(); DecryptDemo();
} private void EncryptDemo()
{
byte[] plainText = Encoding.UTF8.GetBytes("webabcd");
byte[] protectedData = ProtectedData.Protect(plainText, null); _base64 = Convert.ToBase64String(protectedData); MessageBox.Show("字符串“webabcd”加密且base64后的数据:" + Environment.NewLine + _base64);
} private void DecryptDemo()
{
byte[] protectedData = Convert.FromBase64String(_base64);
byte[] plainText = ProtectedData.Unprotect(protectedData, null); MessageBox.Show("解密后的数据:" + Environment.NewLine + Encoding.UTF8.GetString(plainText, , plainText.Length));
}
}
}

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. 演练2-1:创建MVC默认项目

    在VS2012中点击“文件 | 新项目”,在弹出对话框中选择“Visual C# | Web | ASP.NET MVC 4 Web应用程序”. 在弹出的模板对话框中选择“Internet应用程序”和 ...

  2. Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件

    原文 Windows Phone 8初学者开发—第21部分:永久保存Wav音频文件 第21部分:永久保存Wav音频文件 原文地址:http://channel9.msdn.com/Series/Win ...

  3. powerMock比easyMock和Mockito更强大(转)

    powerMock是基于easyMock或Mockito扩展出来的增强版本,所以powerMock分两种类型,如果你习惯于使用easyMock的,那你就下载基于easyMock的powerMock,反 ...

  4. Swift - 获取字符串的MD5值

    MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现. ...

  5. linux下编译.so 和.a 可能出现的问题 ?

    1. 静态函数库 这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持 ...

  6. 2014/08/24——升级stepbystep修复tc不刷新问题并加入杭电bc

    问题: 自从tc站点升级以后做题统计的tc一栏就不刷新了,为此全哥也更新了一下stepbystep的配置文件什么的,我仅仅要将其挂到server上即可了. 由于加了杭电的bc,看来这事儿不easy.还 ...

  7. Eclipse用法和技巧十五:自动添加未实现方法1

    java代码中经常要实现一些接口,这个也是java代码独有的地方.实现接口,就意味着要实现这个接口中定义的方法,如果一个个去码出方法就需要记得方法名称等等,就算有内容辅助快捷键帮助,也是很麻烦的.这里 ...

  8. Java Thread 那些事

    这篇文章被压在草稿箱许久,最近公司内部的技术社区有同学贴出了几篇分享 Java线程的文章,发觉有很多概念没有讲清楚,所以花点时间继续撰写,便有了这篇博文. 本文只聚焦 JVM 层面的线程模型,不考虑和 ...

  9. 基于visual Studio2013解决C语言竞赛题之1075大数阶乘

        题目 解决代码及点评 /************************************************************************/ /* ...

  10. Swift - 做一个简单的无线U盘(手机端Http服务器搭建)

    由于iOS系统的封闭性,在数据传输方面十分不方便.不像安卓设备,直接连接电脑就能当U盘使用.所以一般我们如果用iPhone临时存取个东西,要么使用数据线连接iTunes,要么手机电脑都登上QQ,使用Q ...