介紹
重新想象 Windows 8 Store Apps 之 鎖屏
登入鎖屏,擷取當前程式的鎖屏許可權,從鎖屏中移除
發送徽章或文本到鎖屏
將一個 app 的多個 tile 綁定到鎖屏
自訂鎖屏圖片
樣本
1、示範如何登入鎖屏,擷取當前程式的鎖屏許可權,從鎖屏中移除
LockScreen/AccessLockScreen.xaml
<Page x:Class="XamlDemo.LockScreen.AccessLockScreen" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.LockScreen" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnRequestAccess" Content="請求登入鎖屏" Margin="0 10 0 0" Click="btnRequestAccess_Click" /> <Button Name="btnGetAccessStatus" Content="擷取當前程式的鎖屏許可權" Margin="0 10 0 0" Click="btnGetAccessStatus_Click" /> <Button Name="btnRemoveAccess" Content="從鎖屏中移除" Margin="0 10 0 0" Click="btnRemoveAccess_Click" /> </StackPanel> </Grid></Page>
LockScreen/AccessLockScreen.xaml.cs
/* * 示範如何登入鎖屏,擷取當前程式的鎖屏許可權,從鎖屏中移除 * * 註: * 要想請求鎖屏許可權,需要背景工作支援“推播通知”或“控制通道” */ using System;using Windows.ApplicationModel.Background;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls; namespace XamlDemo.LockScreen{ public sealed partial class AccessLockScreen : Page { public AccessLockScreen() { this.InitializeComponent(); } private async void btnRequestAccess_Click(object sender, RoutedEventArgs e) { try { // 向系統請求登入鎖屏,會彈出確認對話方塊 // 需要背景工作支援“推播通知”或“控制通道”,否則會拋出異常 // 不能在模擬器中運行 // 如果 BackgroundAccessStatus 不等於 Unspecified,則即使調用 RequestAccessAsync() 也不會出現對話方塊,需要使用者去“設定”中去添加或移除鎖定畫面 app BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync(); /* * BackgroundAccessStatus - 當前 app 的鎖屏許可權 * Unspecified - 使用者尚未選擇 * Denied - 被使用者拒絕 * AllowedWithAlwaysOnRealTimeConnectivity - 用於允許了,且支援即時串連,即使電量低 * AllowedMayUseActiveRealTimeConnectivity - 用於允許了,且支援即時串連,但是如果電量低則無法即時串連 */ lblMsg.Text = "RequestAccessAsync(): " + status.ToString(); } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } private void btnGetAccessStatus_Click(object sender, RoutedEventArgs e) { try { // 擷取當前應用程式的鎖屏許可權 BackgroundAccessStatus status = BackgroundExecutionManager.GetAccessStatus(); lblMsg.Text = "GetAccessStatus(): " + status.ToString(); } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } private void btnRemoveAccess_Click(object sender, RoutedEventArgs e) { try { // 將當前應用程式從鎖屏中移除 BackgroundExecutionManager.RemoveAccess(); lblMsg.Text = "RemoveAccess()"; } catch (Exception ex) { lblMsg.Text = ex.ToString(); } } }}