C# 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


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

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