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

一般我们在开发Windows Phone App,有时会需要修改锁定画面,而锁定画面的程式码又臭又长,若日後还有很多支APP需要使用到这个功能,岂不是要打很多次?所以我们何不创建一个自定义类别,将锁定画面的功能写一次就好了,日後若有其他专案使用到该功能,我们只要引入Class或Dll参考即可。

?

本篇文章将引导您自制LockScreen 锁定画面类别,从【网路图片】、【Assets资源】、【UI】设定锁定画面。

?

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

参阅 :

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

?

先看如何使用,我们可以透过【Uri】、【WriteableBitmap】、【UIElement】来操作,

Code部分相当的简短,因为我们透过自定义类别来帮我们完成了,此外也可以保持主程式的画面整洁 :

?

   1:  //从Assets中的资源设定锁定画面
   2:  Uri uri = new Uri("Assets/Tiles/FlipCycleTileLarge.png", UriKind.Relative);
   3:  LockScreen.SetBitmap(uri);
   4:  ?
   5:  //从网路图片设定锁定画面
   6:  Uri uri_Net = new Uri("http://ppt.cc/vJH3", UriKind.Absolute);
   7:  LockScreen.SetBitmap(uri_Net);
   8:  ?
   9:  //从UI设定锁定画面
  10:  LockScreen.SetBitmap(LayoutRoot);

?

自定义类别如下,说明一并打在程式码当中,请各位客观慢用 :

?

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

?

如此一来我们就自制LockScreen 锁定画面类别(Class),从【网路图片】、【Assets资源】、【UI】修改锁定画面罗!

?

References :

Suki统整出来的自定义类别

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

?

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

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

SUKI

HOLIESTAR

DotBlogs Tags:

C#

Change LockScreen

Demo

how to change LockScreen

LockScreen Sample

示范教学

修改

无效

范例程式

锁定画面

锁屏画面

关连文章

C# Windows Phone App 开发,将 【清单型态】 的【ListBox】改为【格状型态】,并使用Binding放入资料。

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

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

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

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

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

    原文:C# Windows Phone App 开发,修改[锁定画面],从[Assets].[UI].[网路图片],并解决失灵问题. 一般我们在开发Windows Phone App,有时会希望透过应 ...

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

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

  3. XNA 4.0 环境搭建和 Hello World,Windows Phone 游戏开发

    XNA 4.0 环境搭建和 Hello World,Windows Phone 游戏开发 使用 Scene 类在 XNA 中创建不同的场景(八) 摘要: 平方已经开发了一些 Windows Phone ...

  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. 一个小白App开发需要了解的基本技术

    本文针对小白用户对App做一个简单的介绍,首先要了解App都有哪些类型,不同的类型适用于哪些需求,用户可以根据自己的需求选择不同的App开发. 一 App有哪些形式 WebApp:简单来说,Web A ...

  9. 《Windows IoT 应用开发指南》

    物物互联的时代已经到来,智能家居.智慧校园.智慧交通.可穿戴.无人机.全息投影,各种各样的新名词.黑科技层出不穷.当我们为五年前能够通过手机控制家电而欣喜若狂的时候,可曾憧憬过当前使用增强现实设备完成 ...

随机推荐

  1. jquery中怎么删除<ul>中的整个<li>包括节点

    .$('ul li').remove(); .$('ul li').each(function(){ $(this).remove(); }); .$("ul").find(&qu ...

  2. jQuery 自学笔记—8 常见操作

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使 ...

  3. 如何使用git

    本文不是谈论git具体命令的技术文章. 原文地址:http://blog.csdn.net/ffb/article/details/11206067 我之前发了一条关于git中如何处理中文文件名的微博 ...

  4. 利用Node.js实现模拟Session验证的登陆

    1.身份验证和用户登陆 在一般的Web应用上,假设要实现用户登陆,最经常使用,也是最简单的方法就是使用Session,主要的思路是在Session中保留一些用户身份信息,然后每次在Session中取, ...

  5. fedora 18 源码编译 android 4.0.1

    1.编译环境: 系统:fedora 18 KED 桌面  (Fedora-18-i686-Live-KDE.iso) 处理器:酷睿i5双核 内存: 4GB 硬盘:46GB java版本:java ve ...

  6. UVAlive 2519 Radar Installation (区间选点问题)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  7. selenium webdriver缺陷

    关闭  selenium webdriver缺陷 除了http://573301735.com/?p=5126讲 的,昨天又发现一个让我1个小时生不如死的问题,就是使用两个不同的配置文件来初始化dri ...

  8. 【Android每周专题】触摸屏事件

    本系列文章均为A2BGeek原创,转载务必在明显处注明: 转载自A2BGeek的[Android每周专题]系列,原文链接:http://blog.csdn.net/benbmw2008/article ...

  9. NET工厂模式架构

    NET工厂模式架构 最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有ap ...

  10. A Game of Thrones(11) - Daenerys

    Daenerys Targaryen wed Khal Drogo with fear and barbaric([bɑː'bærɪk]野蛮的,粗野的) splendor(光彩:壮丽) in a fi ...