博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Windows Phone App 开发,自制LockScreen 锁定画面类别(Class),从【网路图片】、【Assets资源】、【UI】修改锁定画面。...
阅读量:7221 次
发布时间:2019-06-29

本文共 6043 字,大约阅读时间需要 20 分钟。

原文:

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

?

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

?

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

参阅 :

?

先看如何使用,我们可以透过【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
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统整出来的自定义类别

?

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

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

SUKI

HOLIESTAR

DotBlogs Tags:

关连文章

你可能感兴趣的文章
Java对象创建
查看>>
android 开发中向文件指定位置写入数据
查看>>
关于maven使用的一些心得
查看>>
nodejs 开发工具 sublime
查看>>
hadoop集群部署注意问题
查看>>
firefox 选中变删除
查看>>
javascript闭包的个人理解
查看>>
使用HBuilder基于HTML5编写新闻客户端APP的一些实验
查看>>
Description Resource Path Location Type The projec
查看>>
JdbcTemplate详解
查看>>
我的友情链接
查看>>
计算子序列和是定值的子序列个数
查看>>
Windows下win32api下载地址
查看>>
Java中读取properties属性配置文件内容方法
查看>>
转载:一台apache主机上如何绑定多域名
查看>>
诞生记
查看>>
vim 纵向编辑
查看>>
FastDFS安装配置
查看>>
OpenCV实现图像的旋转
查看>>
新装linux 优化
查看>>