【UWP开发】自定义锁屏&桌面壁纸

mtobeiyf关注
2015.11.01 00:16:55字数 394阅读 1,249

调用通用的API来设置桌面壁纸,可以实现很多有趣的功能。
在Windows通用平台中,可以使Windows.System.UserProfile命名空间下的类UserProfilePersonalizationSettings来对系统的开始界面背景和锁屏壁纸进行操作。它可以修改锁屏壁纸和桌面壁纸,调用后会返回bool值,如果成功就是true,否则返回false。
在调用UserProfilePersonalizationSettings类前,先访问一下IsSupported方法,看看是否支持该操作。然后通过Current属性可以获取到一个UserProfilePersonalizationSettings实例,之后你就可以设置壁纸了。
调用TrySetLockScreenImageAsync方法设置锁屏壁纸,调用TrySetWallpaperImageAsync方法可以设置桌面壁纸,参数都是用来作为背景的图片文件的StorageFile

 
 

下面通过实例来演示一下如何实现这个功能。

因为UserProfilePersonalizationSettings被封装在Windows.System.UserProfile中,先引用

using Windows.System.UserProfile

设置壁纸

UserProfilePersonalizationSettings setting
= UserProfilePersonalizationSettings.Current;
//实例化对象
bool b = await setting.TrySetLockScreenImageAsync(file);

其中TrySetLockScreenImageAsync(file)是尝试设置文件对象file为锁屏壁纸。

在测试中,我是直接在项目根目录下添加了一个pic.jpg的1280*720的图片文件。(其实应该放Assets里面的)

那么如何导入图片文件呢?
需要用到StorageFileUri
在Class下定义:

private static Uri imgUri = new Uri("ms-appx:///pic.jpg");

这样就有个Uri指向根目录下的pic.jpg文件。

接下来要用到StorageFile来导入文件。

还是要先引用

using Windows.Storage;

从imgUri导入文件,注意要用到异步

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);

下面贴上主要源码。

public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
} private static Uri imgUri = new Uri("ms-appx:///pic.jpg"); private async void button_Click(object sender, RoutedEventArgs e)
{
var msg = new MessageDialog("");
if (!UserProfilePersonalizationSettings.IsSupported())
{
var mess = new MessageDialog("人品太差,不支持哦!");
await mess.ShowAsync();
}
//获取文件
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(imgUri);
//设置背景
UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current;
bool b = await setting.TrySetLockScreenImageAsync(file);
}
}

实际  返回false

C# code

----------------------------------------------

private static Uri imgUri = new Uri("ms-appx:///Assets/pic.jpg");

private async void Button_Click_1(object sender, RoutedEventArgs e)

{

var msg = new MessageDialog("");

if (!UserProfilePersonalizationSettings.IsSupported())

{

var mess = new MessageDialog("人品太差,不支持哦!");

await mess.ShowAsync();

}

img.Source = new BitmapImage(new Uri("ms-appx:///pic.jpg", UriKind.RelativeOrAbsolute));

//获取文件

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///pic.jpg", UriKind.RelativeOrAbsolute));

//设置背景

UserProfilePersonalizationSettings setting = UserProfilePersonalizationSettings.Current;

bool b = await setting.TrySetLockScreenImageAsync(file);

if (b)

{

msg.Content = "set ok!";

await msg.ShowAsync();

}

else

{

msg.Content = "can not set lock screen!";

await msg.ShowAsync();

}

}

uwp 的锁屏功能的更多相关文章

  1. iOS开发——使用技术OC篇&简单九宫格锁屏功能的实现与封装

    简单九宫格锁屏功能的实现与封装 首先来看看最后的实现界面. 在这开始看下面的内容之前希望你能先大概思考活着回顾一下如果 你会怎么做,只要知道大概的思路就可以. 由于iphone5指纹解锁的实现是的这个 ...

  2. CentOS系统里如何正确取消或者延长屏幕保护自动锁屏功能(图文详解)

    不多说,直接上干货! 对于我这里想说的是,分别从CentOS6.X  和  CentOS7.X来谈及. 1. 问题:默认启动屏幕保护 问题描述: CentOS系统在用户闲置一段时间(默认为5分钟)后, ...

  3. 【Centos】Centos7.5取消自动锁屏功能

    目录 00. 目录 01. 问题描述 02. 问题分析 03. 解决办法 04. 附录 00. 目录 @ 参考博客:[Centos]Centos7.5取消自动锁屏功能 01. 问题描述 Centos7 ...

  4. js防windos锁屏功能实现

    <li class="layui-nav-item"> <a href="javascript:;" id="lock"& ...

  5. Android定时锁屏功能实现(AlarmManager定时部分)

    菜鸟入坑记——第一篇 关键字:AlarmManager 一.AlarmManager简介: 参考网址:https://www.jianshu.com/p/8a2ce9d02640        参考网 ...

  6. Android开发之实现锁屏功能

    锁屏须要引入设备超级管理员.在文档Android开发文档的Administration中有具体的说明. Android设备管理系统功能和控制訪问. 主要有一下几个步骤: 1  创建广播接收者,实现De ...

  7. CentOS取消屏幕保护自动锁屏功能

    CentOS系统在用户闲置一段时间(默认为5分钟)后,会启动屏幕保护程序(默认的屏保为黑屏),并要求重新输入密码才能回到原来的桌面. 设置屏幕保护:System -> Preferences - ...

  8. ajax请求锁屏功能

    我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...

  9. 调整Kali Linux的锁屏时间

    调整Kali Linux的锁屏时间   锁屏是保护隐私的一种重要机制.当用户不操作电脑一段时间后,系统会进入锁屏状态.用户需要输入口令,才能重新进入系统.避免因为操作人员离开电脑后,被其他人员利用现有 ...

随机推荐

  1. EXCEL:关键字有重复,其他信息一行多列显示

    =INDEX(A:A,SMALL(IF(MATCH($A$2:$A$13,$A$2:$A$13,0)=ROW($A$2:$A$13)-1,ROW($2:$13),4^8),ROW(1:1)))& ...

  2. shell脚本(5)-shell变量

    一.变量介绍 将一些数据需要临时存放在内存中,以待后续使用时快速读出. 二.变量分类 1.本地变量: 用户私有变量,只有本用户可以使用,保存在家目录下的.bash_profile..bashrc文件中 ...

  3. mybatis-3-核心配置文件

    全局配置文件:mybatis-config.xml 1.引入外部配置文件(properties) date.properties外部配置文件 driver = com.mysql.cj.jdbc.Dr ...

  4. debug:am trace-ipc源码分析

    debug:am trace-ipc源码分析 目录 debug:am trace-ipc源码分析 一.使用 官网介绍 命令提示 小结 二.源码分析 ActivityManagerShellComman ...

  5. P5110 块速递推-光速幂、斐波那契数列通项

    P5110 块速递推 题意 多次询问,求数列 \[a_i=\begin{cases}233a_{i-1}+666a_{i-2} & i>1\\ 0 & i=0\\ 1 & ...

  6. python读取数据写入excel的四种操作

    Python对Excel的读写主要有:xlrd.xlwt.xlutils.openpyxl.xlsxwriter几种 xlutils结合xlrd: 操作的是以xls后缀的excel,读取文件保留原格式 ...

  7. 第二十四篇 -- Cache学习

    Cache存储器 电脑中为高速缓冲存储器,是位于CPU和主存储器DRAM(Dynamic Random Access Memory)之间,规模较小,但速度很高的存储器,通常由SRAM(Static R ...

  8. 版本号是通过import合并而来的,不是继承来的

  9. Docker未授权漏洞(2375)

    漏洞验证 直接使用浏览器访问 http://ip:2335 http://ip:2335/version http://ip:2335/info docker -H tcp://ip:2375 ver ...

  10. Matlab常用函数:二进制和十进制转换,均值,方差

    文章目录 Size s=size(A) [r,c]=size(A) [r,c,m]=size(A) size(A,n) 二进制和十进制转换 dec2bin mean 均值 mean(a,1) mean ...