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

简介: 原文:C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。 一般我们在开发Windows Phone App,有时会希望透过应用程式来改变锁定画面,但是锁定画面的设定有时会发生非常吊诡的现象,譬如锁定画面只会在第一次设定的时候成功,第二次之後全数失败,而且没有发生任何错误,这究竟是为什麽呢?! ? 本篇文章将引导您修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。
原文: 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>

?

就像这个样子

?

image

?

如此一来才能向系统注册,本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


admentorserve.aspx?type=img&z=18&a=11

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网路自动断线的困扰。

目录
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
88 3
|
1月前
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
1月前
|
开发工具 数据安全/隐私保护 Android开发
【教程】APP 开发后如何上架?
【教程】APP 开发后如何上架?
|
1月前
|
API
uni-app 146朋友圈列表api开发
uni-app 146朋友圈列表api开发
18 0
|
25天前
|
移动开发
uni-app使用v-html输出富文本图片溢出解决
uni-app使用v-html输出富文本图片溢出解决
39 1
|
1月前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
1月前
|
Java Android开发 开发者
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
【Uniapp开发】APP的真机调试指南,从开发到上架全过程
36 3
游戏直播APP平台开发多少钱成本:定制与成品源码差距这么大
开发一款游戏直播APP平台所需的费用是多少?对于计划投身这一领域的投资者来说,首要关心的问题之一就是。本文将探讨两种主要的开发模式——定制开发与成品源码二次开发的成本差异及其优劣势。
|
1月前
|
开发框架 移动开发 JavaScript
SpringCloud微服务实战——搭建企业级开发框架(四十六):【移动开发】整合uni-app搭建移动端快速开发框架-环境搭建
正如优秀的软件设计一样,uni-app把一些移动端常用的功能做成了独立的服务或者插件,我们在使用的时候只需要选择使用即可。但是在使用这些服务或者插件时一定要区分其提供的各种服务和插件的使用场景,例如其提供的【uni-starter快速开发项目模版】几乎集成了移动端所需的所有基础功能,使用非常方便,但是其许可协议只允许对接其uniCloud的JS开发服务端,不允许对接自己的php、java等其他后台系统。
145 2
|
1月前
|
移动开发 负载均衡 小程序
代驾app开发丨代驾系统开发玩法详情丨代驾系统开发网页版/H5/小程序及源码部署
**司机/代驾员端**:司机可以通过APP接收订单,查看订单详情、路线和导航,提供现场服务并进行确认。