與眾不同 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
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM