[源码下载]

重新想象 Windows 8 Store Apps (59) - 锁屏

作者:webabcd

介绍
重新想象 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() 也不会出现对话框,需要用户去“设置”中去添加或移除锁屏应用
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();
}
}
}
}

2、演示如何发送徽章或文本到锁屏
LockScreen/SendNotification.xaml

<Page
x:Class="XamlDemo.LockScreen.SendNotification"
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"> <Button Name="btnSendBadge" Content="send badage to lock screen" Click="btnSendBadge_Click" /> <Button Name="btnSendTile" Content="send tile text to lock screen" Margin="0 10 0 0" Click="btnSendTile_Click" /> </StackPanel>
</Grid>
</Page>

LockScreen/SendNotification.xaml.cs

/*
* 演示如何发送徽章或文本到锁屏
*
* 注:
* 如果需要发送文本到锁屏,需要手动在“设置”中将 app 添加到“选择要显示详细状态的应用”中
*
* 另:
* 关于 tile 和 badge 请参见:XamlDemo/Tile
*/ using NotificationsExtensions.BadgeContent;
using NotificationsExtensions.TileContent;
using Windows.UI.Notifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace XamlDemo.LockScreen
{
public sealed partial class SendNotification : Page
{
public SendNotification()
{
this.InitializeComponent();
} private void btnSendBadge_Click(object sender, RoutedEventArgs e)
{
// 发送 badge 到锁屏
BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent();
BadgeNotification badge = badgeContent.CreateNotification();
BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();
badgeUpdater.Update(badge);
} private void btnSendTile_Click(object sender, RoutedEventArgs e)
{
// 发送文本到锁屏,前提是此 app 在“选择要显示详细状态的应用”中
ITileWideSmallImageAndText03 tileContent = TileContentFactory.CreateTileWideSmallImageAndText03();
tileContent.TextBodyWrap.Text = "hello webabcd";
tileContent.Image.Src = "ms-appx:///Assets/Logo.png";
tileContent.RequireSquareContent = false;
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
}
}
}

3、演示如何将一个 app 的多个 tile 绑定到锁屏
LockScreen/MultipleTiles.xaml

<Page
x:Class="XamlDemo.LockScreen.MultipleTiles"
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"> <Button Name="btnOnlyBadge" Content="只能发送 badge 的可以绑定到锁屏的 SecondaryTile" Click="btnOnlyBadge_Click" /> <Button Name="btnBadgeAndText" Content="既能发送 badge 又能发送 text 的可以绑定到锁屏的 SecondaryTile" Margin="0 10 0 0" Click="btnBadgeAndText_Click" /> </StackPanel>
</Grid>
</Page>

LockScreen/MultipleTiles.xaml.cs

/*
* 演示如何将一个 app 的多个 tile 绑定到锁屏
*
* 要想将 SecondaryTile 绑定到锁屏,需要注意:
* 1、需要设置 SecondaryTile 的 LockScreenBadgeLogo
* 2、如果需要文本支持则还需要设置 SecondaryTile 的 LockScreenDisplayBadgeAndTileText 为 true
* 3、需要手动在“设置”中将 SecondaryTile 添加到锁屏,当然如果需要文本支持则需要手动将 app 添加到“选择要显示详细状态的应用”中
*/ using NotificationsExtensions.BadgeContent;
using NotificationsExtensions.TileContent;
using System;
using Windows.UI.Notifications;
using Windows.UI.Popups;
using Windows.UI.StartScreen;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using XamlDemo.Common; namespace XamlDemo.LockScreen
{
public sealed partial class MultipleTiles : Page
{
private string _tile1Id = "";
private string _tile2Id = ""; public MultipleTiles()
{
this.InitializeComponent();
} // 仅支持 badge 的可以登录锁屏的 SecondaryTile
private async void btnOnlyBadge_Click(object sender, RoutedEventArgs e)
{
SecondaryTile secondTile = new SecondaryTile(
_tile1Id,
"testOnlyBadge",
"testOnlyBadge",
"argument1",
TileOptions.ShowNameOnLogo,
new Uri("ms-appx:///Assets/Logo.png")
); // 需要指定 LockScreenBadgeLogo
secondTile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png"); bool isPinned = await secondTile.RequestCreateForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Above);
if (isPinned)
{
BadgeNumericNotificationContent badgeContent = new BadgeNumericNotificationContent();
BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(_tile1Id).Update(badgeContent.CreateNotification());
}
} // 即支持徽章又支持文本的可以登录锁屏的 SecondaryTile
private async void btnBadgeAndText_Click(object sender, RoutedEventArgs e)
{
SecondaryTile secondTile = new SecondaryTile(
_tile2Id,
"testBadgeAndText",
"testBadgeAndText",
"argument2",
TileOptions.ShowNameOnLogo | TileOptions.ShowNameOnWideLogo,
new Uri("ms-appx:///Assets/Logo.png"),
new Uri("ms-appx:///Assets/WideLogo.png")
); // 需要指定 LockScreenBadgeLogo
secondTile.LockScreenBadgeLogo = new Uri("ms-appx:///Assets/BadgeLogo.png");
// 需要设置 LockScreenDisplayBadgeAndTileText 为 true
secondTile.LockScreenDisplayBadgeAndTileText = true; bool isPinned = await secondTile.RequestCreateForSelectionAsync(Helper.GetElementRect((FrameworkElement)sender), Placement.Above);
if (isPinned)
{
ITileWideText03 tileContent = TileContentFactory.CreateTileWideText03();
tileContent.TextHeadingWrap.Text = "hello webabcd";
tileContent.RequireSquareContent = false;
TileUpdateManager.CreateTileUpdaterForSecondaryTile(_tile2Id).Update(tileContent.CreateNotification());
}
}
}
}

4、演示如何自定义锁屏图片
LockScreen/CustomLockScreenImage.xaml

<Page
x:Class="XamlDemo.LockScreen.CustomLockScreenImage"
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"> <Button Name="btnDemo" Content="自定义锁屏图片" Click="btnDemo_Click" /> <Image Name="img" Width="200" Height="200" Margin="0 10 0 0" HorizontalAlignment="Left" /> </StackPanel>
</Grid>
</Page>

LockScreen/CustomLockScreenImage.xaml.cs

/*
* 演示如何自定义锁屏图片
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using XamlDemo.Common; namespace XamlDemo.LockScreen
{
public sealed partial class CustomLockScreenImage : Page
{
public CustomLockScreenImage()
{
this.InitializeComponent();
} private async void btnDemo_Click(object sender, RoutedEventArgs e)
{
if (Helper.EnsureUnsnapped())
{
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
}; StorageFile imageFile = await imagePicker.PickSingleFileAsync();
if (imageFile != null)
{
// 将指定的图片设置为锁屏图片
await Windows.System.UserProfile.LockScreen.SetImageFileAsync(imageFile); // 获取当前的锁屏图片
IRandomAccessStream imageStream = Windows.System.UserProfile.LockScreen.GetImageStream();
if (imageStream != null)
{
BitmapImage lockScreenImage = new BitmapImage();
lockScreenImage.SetSource(imageStream);
img.Source = lockScreenImage;
}
}
}
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (59) - 锁屏的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLock

    [源码下载] 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLoc ...

  3. 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute

    [源码下载] 重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationCont ...

  4. 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务

    [源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...

  5. 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知

    [源码下载] 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 推送通 ...

  6. 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel)

    [源码下载] 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel) 作者:webabcd 介绍重新想象 Windows 8 Store ...

  7. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  8. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  9. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

随机推荐

  1. StringEx

    static class StringEx { public static string MD5(this String str) { byte[] bytes = new MD5CryptoServ ...

  2. 解决CSS移动端1px边框问题

    移动项目开发中,安卓或者IOS等高分辨率屏幕会把1px的border渲染成2px来显示,网上搜了一下,解决方法如下: 一.利用css中的transform的缩放属性解决,推荐这个.如下面代码. < ...

  3. java匿名类

    一般情况下,我们需要声明一个类去继承一个接口,然后再new这个类,赋值给接口.但有时后这个类只会被调用一次,为了调用方便,那么就可以用匿名类来简化这个步骤. interface IKey{ void ...

  4. STAF自动化测试框架

    STAF自动化测试框架介绍 http://baike.baidu.com/link?url=9oPZN3JntRakidI7xizqCbyGRISMvCKGfXHBB_WH7OAkKjAKZjq88q ...

  5. Android JNI 之 JNIEnv 解析

    jni.h文件 : 了解 JNI 需要配合 jni.h 文件, jni.h 是 Google NDK 中的一个文件, 位置是 $/android-ndk-r9d/platforms/android-1 ...

  6. tmux protocol version mismatch (client 7, server 6)

    $ tmux attach protocol version mismatch (client 7, server 6) $ pgrep tmux 3429 $ /proc/3429/exe atta ...

  7. ubuntu下解决wireshark权限问题

    wireshark要监控eth0,但是必须要root权限才行.但是,直接用root运行程序是相当危险,也是非常不方便的. 解决方法如下: 1.添加wireshark用户组 sudo groupadd ...

  8. 看上去很美 国内CDN现状与美国对比

    CDN的理想与现实 多年以前,当<Kingdom of Heaven>这部史诗电影发行的时候,中国的影迷使用电驴和BT来寻找种子,而那个时候,高清也才刚刚进入电影领域,我的同事不惜用自家的 ...

  9. java之内部类(InnerClass)----非静态内部类、静态内部类、局部内部类、匿名内部类

    提起java内裤类(innerClass)很多人不太熟悉,实际上类似的概念在c++里面也有,那就是嵌套类(Nested Class),关于这俩者的区别,在下文中会有对比.内部类从表面上看,就是在类中定 ...

  10. c++类的 static 和const那些事

    1.static成员变量(非const)必须在类外定义,在类中只是作为声明(声明其scope为该类),不能使用类初始化成员列表来初始化,只能在定义的时候初始化. 2.static const的成员变量 ...