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

原文: 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云端空间的【相片】、【影片】画质。

你可能感兴趣的:(windows phone)