原文:C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。

一般我们在开发Windows Phone App,有时会希望透过应用程式来改变锁定画面,但是锁定画面的设定有时会发生非常吊诡的现象,譬如锁定画面只会在第一次设定的时候成功,第二次之後全数失败,而且没有发生任何错误,这究竟是为什麽呢?!

?

本篇文章将引导您修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。

?

制作修改锁定画面的APP必须要先修改【WMAppManifest.xml】

并接着【Tokens】的标签後贴上写下 :

?

   1:  <Extensions>
   2:        <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
   3:  </Extensions>

?

就像这个样子

?

?

如此一来才能向系统注册,本APP需要获得修改锁定画面的权限。

?

然後要稍微提一下程式码的方法,方法呢其实是将图档写入IsolateStorage中,

然後在存取IsolateStorage的档案转换成Uri後再设定成锁定画面,

等会儿您会看见我在程式码中加入了FileNameA和FileNameB的部分,

主要是因为 Lock screen background for Windows Phone 8 中提到

?

Unique image names

If you update the lock screen background image from isolated storage, you'll need to provide a unique file name on each update. An easy way to accomplish this is to implement A/B switching logic for the file names.

?

实作两个档名来放置锁定画面背景的图档,

SUKI有试过,若只使用同一个档名去修改锁定背景图时完全失灵,

锁定背景图不会被更换,但是也不会出现任何的错误讯息

?

所以我们要使用FileNameA与FileNameB

?

接着就是程式码的部份了,说明一一打在程式码内了,客观慢用

?

   1:  private async void SetLockScreen()
   2:  {
   3:      //判断该APP是否已向系统申请修改锁定画面
   4:      var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
   5:      //若尚未申请
   6:      if (!isProvider)
   7:      {
   8:          //跳出视窗询问使用者,是否授权该APP可以修改锁定画面
   9:          var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();
  10:          isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
  11:      }
  12:  ?
  13:      //从Assets中的资源设定锁定画面
  14:      Uri url = new Uri("Assets/Tiles/FlipCycleTileLarge.png", UriKind.Relative);
  15:      BitmapImage bitmapImage = new BitmapImage();
  16:      bitmapImage.CreateOptions = BitmapCreateOptions.None;
  17:      bitmapImage.UriSource = url;
  18:      bitmapImage.ImageOpened += (s, e) =>
  19:      {
  20:          //下载完成
  21:          WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s);
  22:          //将Bitmap转换成WriteableBitmap
  23:          Uri uri_Net = new Uri(WriteImageToFile(new WriteableBitmap(wbm)), UriKind.Absolute);
  24:          //设定锁定画面
  25:          Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri_Net);
  26:      };
  27:  ?
  28:  ?
  29:      //从网路图片设定锁定画面
  30:      Uri url_Net = new Uri("http://ppt.cc/vJH3", UriKind.Absolute);
  31:      BitmapImage bitmapImage_Net = new BitmapImage();
  32:      bitmapImage_Net.CreateOptions = BitmapCreateOptions.None;
  33:      bitmapImage_Net.UriSource = url;
  34:      bitmapImage_Net.ImageOpened += (s, e) =>
  35:      {
  36:          //下载完成
  37:          WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s);
  38:          //将Bitmap转换成WriteableBitmap
  39:          Uri uri_Net = new Uri(WriteImageToFile(new WriteableBitmap(wbm)), UriKind.Absolute);
  40:          //设定锁定画面
  41:          Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri_Net);
  42:      };
  43:              
  44:  ?
  45:  ?
  46:      //从UI设定锁定画面
  47:      Uri uri_UI = new Uri(WriteImageToFile(new WriteableBitmap(LayoutRoot, null)), UriKind.Absolute);
  48:      Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri_UI);
  49:  }
  50:  ?
  51:          
  52:  ?
  53:  //档案写入Isolate 回传 Uri路径
  54:  private string WriteImageToFile(WriteableBitmap writeable_bitmap)
  55:  {
  56:      //档名A
  57:      string FileNameA = "A.jpg";
  58:      //档名B
  59:      string FileNameB = "B.jpg";
  60:      //最後使用的党名
  61:      string FileName = "";
  62:      try
  63:      {
  64:  ?
  65:          using (IsolatedStorageFile tStorage = IsolatedStorageFile.GetUserStoreForApplication())
  66:          {
  67:              //宣告存取IsolatedStorageFile的变数
  68:              var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
  69:  ?
  70:              //若为第一次A、B都不存在
  71:              if (!isolatedStorage.FileExists(FileNameA) && !isolatedStorage.FileExists(FileNameB))
  72:              {
  73:                  //使用其中一个当作档名
  74:                  FileName = FileNameA;
  75:              }
  76:              //若A存在则使用B名称来当作写入的档名
  77:              if (isolatedStorage.FileExists(FileNameA))
  78:              {
  79:                  //删除A
  80:                  isolatedStorage.DeleteFile(FileNameA);
  81:                  //使用档名B
  82:                  FileName = FileNameB;
  83:              }
  84:              //若B存在则使用A名称来当作写入的档名
  85:              if (isolatedStorage.FileExists(FileNameB))
  86:              {
  87:                  //删除B
  88:                  isolatedStorage.DeleteFile(FileNameB);
  89:                  //使用档名A
  90:                  FileName = FileNameA;
  91:              }
  92:                     
  93:              Debug.WriteLine(FileName);
  94:              //在独立存储区创建档案
  95:              IsolatedStorageFileStream fileStream = isolatedStorage.CreateFile(FileName);
  96:              //写入JPG图档,品质为100 (越低图片画质就越低)
  97:              writeable_bitmap.SaveJpeg(fileStream, writeable_bitmap.PixelWidth, writeable_bitmap.PixelHeight, 0, 100);
  98:              //关闭IO
  99:              fileStream.Close();
 100:              fileStream.Dispose();
 101:              tStorage.Dispose();
 102:  ?
 103:          }
 104:          //重组新的URI,并回传
 105:          return string.Format("ms-appdata:///local/{0}", FileName);
 106:      }
 107:      catch (Exception ex)
 108:      {
 109:          string tMsg = ex.Message;
 110:          return string.Empty;
 111:      }
 112:  }

?

如此一来修改锁定画面就是一件简单的事情了!

?

References :

Windows Phone 8 锁屏背景与通知

WP8 LockScreen IsolatedStorageFile 锁定屏幕 不会改变 C#

?

文章中的叙述如有观念不正确错误的部分,欢迎告知指正 谢谢

转载请注明出处,并且附上本篇文章网址 !? 感谢。

SUKI

HOLIESTAR

DotBlogs Tags:

C#

Change LockScreen

Demo

how to change LockScreen

LockScreen

Sample

示范教学

修改

无效

范例程式

锁定画面

锁屏画面

关连文章

[笔记]C# Windows Phone App 开发,邀请使用者对APP评分。

C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从【网路图片】、【Assets资源】、【UI】修改锁定画面。

Windows Phone 使用,改善Windows Phone 将照片同步到SkyDrive云端空间的【相片】、【影片】画质。

Windows Phone 使用,解决待机时WIFI网路自动断线的困扰。

C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。的更多相关文章

  1. C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从【网路图片】、【Assets资源】、【UI】修改锁定画面。

    原文:C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从[网路图片].[Assets资源].[UI]修改锁定画面. 一般我们在开发Windows ...

  2. 打造理想的Windows 10 APP开发环境的5个步骤

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:微软即将发布Windows 10手机版,实际上很多人现在已经开始在开发Windows ...

  3. APP开发中的弹窗体系,UI设计师不能忽视的地方

    1. 弹窗的定义 弹窗分为模态弹窗和非模态弹窗两种. 弹窗分类 模态弹窗:很容易打断用户的操作行为,用户必须回应,否则不能进行其他操作. 非模态弹窗:不会影响用户的操作,用户可以不对其进行回应,非模态 ...

  4. Windows Phone Studio-任何人都能开发Windows Phone App的在线工具

    在一段时间的内测以后,微软于今天早些时候发布了其Windows Phone应用开发的在线工具,名字叫做Windows Phone Studio.其意义在于,通过简单的内容添加和样式选择,实现Windo ...

  5. Windows 8.1 store app 开发笔记

    原文:Windows 8.1 store app 开发笔记 零.简介 一切都要从博彦之星比赛说起.今年比赛的主题是使用Bing API(主要提到的有Bing Map API.Bing Translat ...

  6. 【万里征程——Windows App开发】控件大集合2

    以下再来看看一些前面还没有讲过的控件,只是控件太多以至于无法所有列出来,大家仅仅好举一反三啦. Button 前面最经常使用的控件就是Button啦,Button另一个有意思的属性呢.当把鼠标指针放在 ...

  7. Windows App开发之文件与数据

    读取文件和目录名 这一节開始我们将陆续看到Windows App是如何操作文件的. 在Windows上读取文件名称.目录名 首先我们在XAML中定义一个Button和TextBlock,将读取文件/目 ...

  8. 20个可以帮你简化iOS app开发流程的工具

    这里推荐20个可以帮你简化iOS app开发流程的工具.很多开发者都使用过这些工具,涉及原型和设计.编程.测试以及最后的营销,基本上涵盖了整个开发过程. 原型和设计 有了一个很好的创意后,你要做的不是 ...

  9. iOS开发之工具篇-20个可以帮你简化移动app开发流程的工具

    如果想进入移动app开发这个领域,你总能从别的开发者或者网上或者书上找到各种各样的方法和工具,对于新手来说,还没有摸清门路就已经陷入迷茫了.这里推荐20个可以帮你简化app开发流程的工具.很多开发者都 ...

随机推荐

  1. gcc -D 传值给代码,默认值为1

    gcc -D 传值给代码,默认值为1 -D 参数可以给代码中的宏打开一扇门.简单的代码#include <stdio.h> #ifdef WHO #define NAME "jo ...

  2. How to get the source code of the chromium of the specified revision

    I'd like to get the source code of the chromium 34.0.1847.9. gclient config http://src.chromium.org/ ...

  3. Unity3D 游戏开发构架篇 ——输入控制

    临近毕业之初.进入Unity3D这个行业,是一家小工作室.老板人非常不错,公司氛围也非常单纯.近期公司开发一款小游戏,初次上手,颇多周折,记录下自己的开发心得.主要涉及一些设计理念,互相交流. 先说下 ...

  4. DMA过程分析

    1.1 当我们在应用程序中编写write系统调用,向磁盘中写入数据时,写入请求会先调用底层写函数,将请求先写入内存中的页快速缓存(page cache)中,写入成功则立马返回,真正的写入磁盘操作会延迟 ...

  5. python glob 用通配符查找指定目录中的文件 - 开源中国社区

    python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件

  6. 用jsp写注冊页面

    包含单选框.多选框.session的应用,页面自己主动跳转,中文乱码的处理,入门级 对于中文乱码的处理,注意几点:注冊页面数据提交方式为post不能忘了写,页面编码方式为gbk,处理提交信息的doRe ...

  7. GMM的EM算法

    在聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut一文中我们给出了GMM算法的基本模型与似然函数,在EM算法原理中对EM算法的实现与收敛性证明 ...

  8. sprintf,多少钱你知道?

    选<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总号1 期 - 93 -笔者:steedhorse(晨星)printf 可能是很 ...

  9. Graphviz 绘制流程图

    凝视说明非常具体.不再详述. digraph G{ //dot 是一种画图语言,它能够方便你採用图形的方式高速.直观地表达一些想法, //比方描写叙述某个问题的解决方式,构思一个程序的流程,澄清一堆貌 ...

  10. 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)

    近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...