與眾不同 windows phone (47) - 8.0 其它: 鎖屏信息和鎖屏背景, 電池狀態, 多分辨率, 商店, 內置協議, 快速恢復
作者:webabcd
介紹
與眾不同 windows phone 8.0 之 其它
- 鎖屏信息和鎖屏背景
- 電池狀態
- 多分辨率應用
- 與 Windows Phone 商店相關的操作
- 系統的內置協議
- 快速恢復應用
- App.xaml.cs 的說明
- manifest 的說明
示例
1、演示如何發送信息到鎖屏,以及如何修改鎖屏背景
Others/LockScreen.xaml
<phone:PhoneApplicationPage x:Class="Demo.Others.LockScreen" 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" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel> <TextBlock Name="lblMsg" TextWrapping="Wrap" /> <Button Name="btnGotoLockScreen" Content="跳轉到鎖屏設置界面" Click="btnGotoLockScreen_Click" /> <Button Name="btnSendMessageToLockScreen" Content="發送信息到鎖屏" Click="btnSendMessageToLockScreen_Click" /> <Button Name="btnLockScreenBackground" Content="修改鎖屏背景" Click="btnLockScreenBackground_Click" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
Others/LockScreen.xaml.cs
/* * 演示如何發送信息到鎖屏,以及如何修改鎖屏背景 * * * 注:在 manifest 中需要增加的內容和說明如下 * 1、搜索並修改“<DeviceLockImageURI />”節點的內容如下: * <!--鎖屏圖標:38 * 38 像素,僅支持白色和透明--> * <DeviceLockImageURI IsRelative="true" IsResource="false">Assets\LockScreen.png</DeviceLockImageURI> * * 2、在 <Extensions /> 節點中增加內容如下: * <!--支持鎖屏上顯示圖標和數字--> * <Extension TaskID="_default" ExtensionName="LockScreen_Notification_IconCount" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" /> * <!--支持鎖屏上顯示文本--> * <Extension TaskID="_default" ExtensionName="LockScreen_Notification_TextField" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" /> * <!--支持鎖屏背景的修改--> * <Extension TaskID="_default" ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" /> */ using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using Windows.System; using Windows.Phone.System.UserProfile; namespace Demo.Others { public partial class LockScreen : PhoneApplicationPage { public LockScreen() { InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 在鎖屏設置界面,如果將本 app 設置為背景提供程序,則鎖屏界面上會有一個名為“打開應用”的按鈕,點擊后會通過 /MainPage.xaml?WallpaperSettings=1 啟動本 app // 相關的 UriMapper 參見 MyUriMapper.cs IDictionary<string, string> queryStrings = this.NavigationContext.QueryString; if (queryStrings.ContainsKey("WallpaperSettings")) lblMsg.Text = "在鎖屏界面啟動了本 app,啟動的 url 是:" + e.Uri.ToString(); base.OnNavigatedTo(e); } // 跳轉到鎖屏設置界面 private async void btnGotoLockScreen_Click(object sender, RoutedEventArgs e) { bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-lock:")); } // 發送信息到鎖屏 private void btnSendMessageToLockScreen_Click(object sender, RoutedEventArgs e) { // 鎖屏信息來自 tile(StandardTileData, FlipTile, IconicTile, CycleTile 均可) ShellTile shellTile = ShellTile.ActiveTiles.First(); if (shellTile != null) { StandardTileData tile = new StandardTileData(); tile.BackContent = "發送信息到鎖屏"; // 需要在鎖屏上顯示的文本內容(需要在鎖屏界面設置本 app 為顯示詳細狀態) tile.Count = 10; // 需要在鎖屏上顯示的數字內容,圖標來自 <DeviceLockImageURI /> 節點的設置(需要在鎖屏界面設置本 app 為顯示即時狀態) shellTile.Update(tile); } } // 修改鎖屏背景 private async void btnLockScreenBackground_Click(object sender, RoutedEventArgs e) { try { /* * 注: * 在項目根目錄下增加 DefaultLockScreen.jpg 文件,用於當 app 成為鎖屏背景的實際提供程序卻沒有設置鎖屏背景時,作為鎖屏背景的默認圖片 * 比如用戶在鎖屏設置頁面選擇了本 app 作為鎖屏背景的實際提供程序,此時本 app 無法設置鎖屏背景,那么鎖屏背景圖就會被設置為項目根目錄下的 DefaultLockScreen.jpg 文件 */ // 判斷本 app 是否是鎖屏背景的實際提供程序 bool isProvider = LockScreenManager.IsProvidedByCurrentApplication; if (!isProvider) { // 請求成為鎖屏背景的實際提供程序(會彈出一個對話框) LockScreenRequestResult result = await LockScreenManager.RequestAccessAsync(); // LockScreenRequestResult.Granted - 用戶已允許;LockScreenRequestResult.Denied - 用戶已拒絕 isProvider = result == LockScreenRequestResult.Granted; } if (isProvider) { // 圖片地址支持“ms-appx:///”和“ms-appdata:///Local/”,文件名必須與當前鎖屏背景的文件名不同 Uri uri = new Uri("ms-appx:///Assets/AppTile.png", UriKind.Absolute); // 設置當前鎖屏的背景圖 Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); // 獲取當前鎖屏的背景圖的 uri Uri currentUri = Windows.Phone.System.UserProfile.LockScreen.GetImageUri(); lblMsg.Text = "當前鎖屏的背景圖的 url: " + currentUri.ToString(); } else { lblMsg.Text = "用戶不允許此 app 成為鎖屏背景的實際提供程序"; } } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } } }
2、演示如何獲取電池的狀態信息
Others/BatteryDemo.xaml.cs
/* * 演示如何獲取電池的狀態信息 */ using System; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Windows.Phone.Devices.Power; using Microsoft.Phone.Info; namespace Demo.Others { public partial class BatteryDemo : PhoneApplicationPage { private Battery _battery; public BatteryDemo() { InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 獲取電話的默認 Battery 對象 _battery = Battery.GetDefault(); // 獲取電池在完全用完之前的所剩時間 TimeSpan remainingTime = _battery.RemainingDischargeTime; // 獲取剩余電量的百分比 int remainingChargePercent = _battery.RemainingChargePercent; // 判斷當前是電池供電還是外部電源供電 lblMsg.Text = "PowerSource: " + DeviceStatus.PowerSource.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += "剩余時間:" + string.Format("{0}{1}{2}{3}{4}{5}", remainingTime.Days, "天", remainingTime.Hours, "小時", remainingTime.Minutes, "分"); lblMsg.Text += Environment.NewLine; lblMsg.Text += "電量百分比:" + remainingChargePercent.ToString() + "%"; // 剩余電量的百分比發生變化時所觸發的事件 _battery.RemainingChargePercentChanged += _battery_RemainingChargePercentChanged; base.OnNavigatedTo(e); } protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { _battery.RemainingChargePercentChanged -= _battery_RemainingChargePercentChanged; base.OnNavigatingFrom(e); } void _battery_RemainingChargePercentChanged(object sender, object e) { // 剩余電量的百分比發生了變化 } } }
3、演示如何獲取當前分辨率
Others/MultiResolution.xaml
<phone:PhoneApplicationPage x:Class="Demo.Others.MultiResolution" 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" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel> <TextBlock TextWrapping="Wrap"> <Run>1、WVGA - 480 x 800; WXGA - 768 x 1280; 720p - 720 x 1280</Run> <LineBreak /> <Run>2、大多數情況下,建議使用 WXGA 資產,他們會在其他分辨率下自動縮放且顯示良好</Run> <LineBreak /> <Run>3、如果需要不同分辨率使用不同資產,則必須寫代碼,參見 code-behind 中判斷當前分辨率的代碼</Run> <LineBreak /> <Run>4、適應不同分辨率的啟動屏幕,需要在項目的根目錄下放置三個啟動屏幕文件:SplashScreenImage.Screen-WVGA.jpg, SplashScreenImage.Screen-WXGA.jpg, plashScreenImage.Screen-720p.jpg</Run> <LineBreak /> <Run>5、如果只想用一個啟動屏幕文件的話,則在項目的根目錄下放置一個分辨率為 768 x 1280 的名為 SplashScreenImage.jpg 的文件即可(不同分辨率會自動縮放)</Run> </TextBlock> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="0 10 0 0" /> </StackPanel> </Grid> </phone:PhoneApplicationPage>
Others/MultiResolution.xaml.cs
/* * 演示如何獲取當前分辨率 */ using System.Windows.Navigation; using Microsoft.Phone.Controls; namespace Demo.Others { public partial class MultiResolution : PhoneApplicationPage { public MultiResolution() { InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 獲取當前的分辨率 lblMsg.Text = "Resolution: " + ResolutionHelper.CurrentResolution.ToString(); base.OnNavigatedTo(e); } } public enum Resolution { WVGA, WXGA, HD720p, Unknown }; public static class ResolutionHelper { /// <summary> /// 480 x 800 /// </summary> private static bool IsWvga { get { return App.Current.Host.Content.ScaleFactor == 100; } } /// <summary> /// 768 x 1280 /// </summary> private static bool IsWxga { get { return App.Current.Host.Content.ScaleFactor == 160; } } /// <summary> /// 720 x 1280 /// </summary> private static bool Is720p { get { return App.Current.Host.Content.ScaleFactor == 150; } } public static Resolution CurrentResolution { get { if (IsWvga) return Resolution.WVGA; else if (IsWxga) return Resolution.WXGA; else if (Is720p) return Resolution.HD720p; else return Resolution.Unknown; } } } }
4、演示與 Windows Phone 商店相關的操作
Others/StoreDemo.xaml.cs
/* * 演示與 Windows Phone 商店相關的操作 * * 注: * 應用內購買測試請參考:http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj681689 */ using Microsoft.Phone.Controls; using Windows.ApplicationModel.Store; namespace Demo.Others { public partial class StoreDemo : PhoneApplicationPage { public StoreDemo() { InitializeComponent(); CurrentAppDemo(); LicenseInformationDemo(); } private void CurrentAppDemo() { // 由 Windows 商店創建的此 app 的 id // CurrentApp.AppId; // 獲取此 app 在 Windows 商店中的 URI 地址 // CurrentApp.LinkUri; // 獲取當前 app 的 LicenseInformation 對象 // CurrentApp.LicenseInformation; // 請求應用程序內購買完整許可證(wp 目前不可用) // CurrentApp.RequestAppPurchaseAsync(); // 請求應用程序內購買指定產品的許可證 // CurrentApp.RequestProductPurchaseAsync // 獲取此 app 的全部購買記錄(wp 目前不可用) // CurrentApp.GetAppReceiptAsync(); // 獲取此 app 的指定產品的購買記錄 // CurrentApp.GetProductReceiptAsync(); // 獲取此 app 的 ListingInformation 信息 // CurrentApp.LoadListingInformationAsync(); // 獲取此 app 的指定關鍵字的 ListingInformation 信息 // CurrentApp.LoadListingInformationByKeywordsAsync(); // 獲取此 app 的指定產品 Id 的 ListingInformation 信息 // CurrentApp.LoadListingInformationByProductIdsAsync(); // 通知商城本 app 已將指定產品 Id 的產品交付給了用戶,在調用此方法之前,用戶不能再次購買此產品 // CurrentApp.ReportProductFulfillment(); // ListingInformation - 此 app 在 Windows 商店中的相關信息 // AgeRating - app 的年齡分級 // CurrentMarket - app 的當前商店,如:en-us // Name - app 在當前商店中的名稱 // FormattedPrice - app 在當前商店中的價格 // Description - app 在當前商店中的程序說明 // ProductListings - app 在當前商店中的 ProductListing 集合 // ProductListing - app 的產品信息 // ProductId - 產品 ID // Name - 產品名稱 // FormattedPrice - 產品當前市場的格式化后的價格 // ImageUri - 產品關聯的圖片的 uri // Description - 產品說明 // Keywords - 搜索關鍵字 // ProductType - 產品類型 // Unknown - 未知 // Durable - 耐用型:用戶購買后由用戶永遠所有的產品) // Consumable - 消費型:可以購買、使用(消耗)和再度購買的產品 // Tag - 自定義標記字符串 } private void LicenseInformationDemo() { // 當前 app 的許可證信息 LicenseInformation licenseInformation = CurrentApp.LicenseInformation; // 許可證ok則為true,否則無許可證或許可證過期則為false // licenseInformation.IsActive; // 是否是試用許可證 // licenseInformation.IsTrial; // 許可證的到期時間 // licenseInformation.ExpirationDate; // 許可證狀態發生變化時所觸發的事件 // licenseInformation.LicenseChanged; // 獲取此 app 相關的 ProductLicenses 集合 // licenseInformation.ProductLicenses; // ProductLicense - 產品許可證 // ProductId - 產品 ID // IsActive - 此產品許可證ok則為true,否則無此產品許可證或此產品許可證過期則為false // IsConsumable - 產品類型是否是消費型 // ExpirationDate - 此產品許可證的到期時間 } } }
5、演示系統的內置協議(uri 關聯)
Others/ReservedProtocol.xaml.cs
/* * 演示系統的內置協議(uri 關聯) * * * 注:系統保留的文件類型和保留的協議類型詳見如下鏈接 * http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/jj207065 */ using System; using System.Windows; using Microsoft.Phone.Controls; using Windows.System; namespace Demo.Others { public partial class ReservedProtocol : PhoneApplicationPage { public ReservedProtocol() { InitializeComponent(); } private async void btnAirplane_Click(object sender, RoutedEventArgs e) { // 跳轉到“飛行模式設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-airplanemode:")); } private async void btnBluetooth_Click(object sender, RoutedEventArgs e) { // 跳轉到“藍牙設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-bluetooth:")); } private async void btnCellular_Click(object sender, RoutedEventArgs e) { // 跳轉到“手機網絡設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-cellular:")); } private async void btnWiFi_Click(object sender, RoutedEventArgs e) { // 跳轉到“WiFi設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-wifi:")); } private async void btnLocation_Click(object sender, RoutedEventArgs e) { // 跳轉到“定位設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-location:")); } private async void btnEmailAccount_Click(object sender, RoutedEventArgs e) { // 跳轉到“電子郵件+賬戶設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-emailandaccounts:")); } private async void btnLockScreen_Click(object sender, RoutedEventArgs e) { // 跳轉到“鎖屏設置”頁 bool success = await Launcher.LaunchUriAsync(new Uri("ms-settings-lock:")); } private async void btnCallTo_Click(object sender, RoutedEventArgs e) { // 撥打指定的電話號碼 bool success = await Launcher.LaunchUriAsync(new Uri("tel:1391234567")); // bool success = await Launcher.LaunchUriAsync(new Uri("callto:1391234567")); } private async void btnHttp_Click(object sender, RoutedEventArgs e) { // 用瀏覽器打開一個指定的 http 鏈接 bool success = await Launcher.LaunchUriAsync(new Uri("http://webabcd.cnblogs.com/")); } private async void btnMailTo_Click(object sender, RoutedEventArgs e) { // 向指定的地址發送郵件 bool success = await Launcher.LaunchUriAsync(new Uri("mailto:aaa@xxx.com")); } private async void btnMarketplaceDetail_Click(object sender, RoutedEventArgs e) { // 打開指定 app 在商店中的詳細頁 bool success = await Launcher.LaunchUriAsync(new Uri("zune:navigate?appid=02adaaff-ad55-42b4-9ca1-fd4d1dadd32a")); } private async void btnMarketplaceReview_Click(object sender, RoutedEventArgs e) { // 在商店中評論指定的 app bool success = await Launcher.LaunchUriAsync(new Uri("zune:reviewapp?appid=02adaaff-ad55-42b4-9ca1-fd4d1dadd32a")); } private async void btnMarketplaceSearch_Click(object sender, RoutedEventArgs e) { // 在商店中搜索 app(支持按關鍵字和發行商搜索) bool success = await Launcher.LaunchUriAsync(new Uri("zune:search?keyword=&publisher=webabcd&contenttype=app")); } } }
6、演示什么是快速恢復應用,以及如何設置快速恢復應用
Others/FastResume.xaml
<phone:PhoneApplicationPage x:Class="Demo.Others.FastResume" 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" shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent"> <StackPanel> <TextBlock TextWrapping="Wrap"> <Run>1、什么是快速恢復應用:當你點擊 tile 啟動了 app 后,再回到開始屏幕,再點擊 tile 可以直接恢復應用而不是重新啟動應用(wp8 新特性)</Run> <LineBreak /> <Run>2、如何實現快速恢復應用:在 manifest 中的 DefaultTask 節點中增加 ActivationPolicy="Resume"</Run> </TextBlock> </StackPanel> </Grid> </phone:PhoneApplicationPage>
7、App.xaml.cs 的說明
Others/AppXamlCs.xaml
<phone:PhoneApplicationPage x:Class="Demo.Others.AppXamlCs" 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" shell:SystemTray.IsVisible="True"> <Grid Background="Transparent"> <StackPanel> <TextBlock x:Name="lblMsg" TextWrapping="Wrap"> <Run>關於 App.xaml.cs 的說明,參見它的注釋就好,寫得很清楚了</Run> <LineBreak /> <Run>注意:導航過程中發生異常會通過 RootFrame_NavigationFailed 捕獲,其他未處理異常會通過 Application_UnhandledException 捕獲</Run> </TextBlock> </StackPanel> </Grid> </phone:PhoneApplicationPage>
8、manifest 的說明
Others/Manifest.xaml
<phone:PhoneApplicationPage x:Class="Demo.Others.Manifest" 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" shell:SystemTray.IsVisible="True"> <Grid Background="Transparent"> <StackPanel> <TextBlock x:Name="lblMsg" TextWrapping="Wrap"> <Run>關於 manifest 的說明,詳見 WMAppManifest.xml</Run> <LineBreak /> <Run>1、應用程序 UI: UI 相關的設置</Run> <LineBreak /> <Run>2、功能: 勾選你需要用到的功能,有時提示無權限可能就是相關的功能沒有勾選</Run> <LineBreak /> <Run>3、要求: 勾選你程序的硬件要求,可以防止不符合要求的設備安裝</Run> <LineBreak /> <Run>4、打包: 打包就是打包了</Run> </TextBlock> </StackPanel> </Grid> </phone:PhoneApplicationPage>
OK
[源碼下載]
