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

简介: 原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密[索引页][源码下载] 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用...
原文: 与众不同 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, 0, plainText.Length));
        }
    }
}



OK
[源码下载]

目录
相关文章
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
473 0
|
8月前
|
传感器 机器学习/深度学习 算法
【室内导航通过视觉惯性数据融合】将用户携带的智能手机收集的惯性数据与手机相机获取的视觉信息进行融合研究(Matlab代码实现)
【室内导航通过视觉惯性数据融合】将用户携带的智能手机收集的惯性数据与手机相机获取的视觉信息进行融合研究(Matlab代码实现)
260 2
|
12月前
|
安全 测试技术 Linux
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
370 2
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
|
存储 监控 安全
如何排查常见的 Windows 应用程序错误和崩溃
本文介绍了如何通过事件日志分析来诊断Windows应用程序错误和崩溃的根本原因。文章首先解释了应用错误的表现形式及常见事件ID(如1000、1001等),并分析了导致崩溃的原因,包括硬件问题(如存储不足、外部因素)和软件问题(如编码错误、数据损坏、.NET Framework兼容性)。接着,提供了几种故障排除方法,例如运行系统文件检查器(SFC)、执行干净启动、检查更新以及重新安装.NET Framework。最后,探讨了使用日志管理工具(如EventLog Analyzer)集中分析崩溃事件的功能,包括预置报表、时间轴分析、实时警报和自动化响应,帮助管理员高效解决应用问题。
2239 1
|
移动开发 数据安全/隐私保护
ClKLog支持手机端查询统计数据啦!
ClKLog的付费版中提供了兼容移动端的h5展示界面,简单来说,手机浏览器直接访fangwe问统计地址就能直接查询主要的统计数据。
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
3735 5
|
数据库 数据安全/隐私保护 Windows
Windows远程桌面出现CredSSP加密数据修正问题解决方案
【10月更文挑战第30天】本文介绍了两种解决Windows系统凭据分配问题的方法。方案一是通过组策略编辑器(gpedit.msc)启用“加密数据库修正”并将其保护级别设为“易受攻击”。方案二是通过注册表编辑器(regedit)在指定路径下创建或修改名为“AllowEncryptionOracle”的DWORD值,并将其数值设为2。
12073 3
|
数据采集 存储 XML
python实战——使用代理IP批量获取手机类电商数据
本文介绍了如何使用代理IP批量获取华为荣耀Magic7 Pro手机在电商网站的商品数据,包括名称、价格、销量和用户评价等。通过Python实现自动化采集,并存储到本地文件中。使用青果网络的代理IP服务,可以提高数据采集的安全性和效率,确保数据的多样性和准确性。文中详细描述了准备工作、API鉴权、代理授权及获取接口的过程,并提供了代码示例,帮助读者快速上手。手机数据来源为京东(item.jd.com),代理IP资源来自青果网络(qg.net)。
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
419 9
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
1892 1

热门文章

最新文章