设为首页 收藏本站
查看: 947|回复: 0

重新想象 Windows 8 Store Apps (59)

[复制链接]

尚未签到

发表于 2015-5-22 06:32:11 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (59) - 锁屏  
作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 锁屏


  • 登录锁屏,获取当前程序的锁屏权限,从锁屏中移除
  • 发送徽章或文本到锁屏
  • 将一个 app 的多个 tile 绑定到锁屏
  • 自定义锁屏图片
  
示例
1、演示如何登录锁屏,获取当前程序的锁屏权限,从锁屏中移除
LockScreen/AccessLockScreen.xaml













  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











  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(3);
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











  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 = "123";
private string _tile2Id = "456";
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(2);
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











  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
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69305-1-1.html 上篇帖子: Windows phone 8 学习笔记(2) 数据文件操作 下篇帖子: 重新想象 Windows 8 Store Apps (39)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表