wp8.1 创建动态磁贴应用
目前Windows Phone 8.1所支持磁贴像素大小有71x71、150x150和310x150,分为大中小三种模式,对于桌面磁贴微软提供的诸多模板http://msdn.microsoft.com/zh-cn/library/windows/apps/hh761491.aspx,模板功能比较简单,就是图像和文字的结合,如果要实现复杂的磁贴组合,比较天气预报那样的磁贴,那就要微软RenderTargetBitmap类事先生成图像,再更新到磁贴。

我们写一个静态方法。在使用RenderTargetBitmap类的同时,建议去看一下微软的关于此类的用法,参数中的element元素也有所限制。
1.一些视频无法生成图像。
2.自定义一些第三方插件无法生成图像。
4.元素必须要在屏幕元素的视觉树中,比如动态创建一个元素,那么就无法生成该元素的图像,出现空白。建议预先将元素写在xaml中并设置opacity为0即可。
public static async System.Threading.Tasks.Task RenderImage(Windows.UI.Xaml.UIElement element, string filename)
{
Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap renderTargetBitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(element, (int)element.DesiredSize.Width, (int)element.DesiredSize.Height);
Windows.Storage.Streams.IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
var width = renderTargetBitmap.PixelWidth;
var height = renderTargetBitmap.PixelHeight; Windows.Storage.StorageFile storageFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.ReplaceExisting);
using (var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
{
var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Ignore,
(uint)width, (uint)height, , , pixelBuffer.ToArray());
await encoder.FlushAsync();
}
}
那么在xaml我们可以这样做。
<Grid Opacity="0">
<Grid x:Name="Tile310x150f" Width="310" Height="150">
<!--内容-->
</Grid>
<Grid x:Name="Tile310x150b" Width="310" Height="150">
<!--内容-->
</Grid>
</Grid>
现在我们在后台来刷新图像。
public async Task RefreshImage()
{
Grid render310x150f = this.ViewControl.FindName("Tile310x150f") as Grid;
await Common.Common.RenderImage(render310x150f, "Tile310x150f.jpg"); Grid render310x150b = this.ViewControl.FindName("Tile310x150b") as Grid;
await Common.Common.RenderImage(render310x150b, "Tile310x150b.jpg");
}
然后pin到桌面。这里提醒一点,RefreshImage要在RequestCreateAsync前执行完成,因为当启用磁贴pin时,页面会跳转到桌面,否则会发生截图空白。
public async void PinTile(string tileId)
{
Uri uri = new Uri("ms-appdata:///local/Tile71x71f.jpg");
Windows.UI.StartScreen.SecondaryTile tile = new Windows.UI.StartScreen.SecondaryTile(tileId, "", "/MainPage.xaml", uri, Windows.UI.StartScreen.TileSize.Default);
tile.VisualElements.Wide310x150Logo = uri;
tile.DisplayName = " "; await RefreshImage(); this.isPin = await tile.RequestCreateAsync();
this.PinVisible = Visibility.Collapsed;
this.UnpinVisible = Visibility.Visible; this.UpdateTile();
}
接下来是update tile的方法,这里的GetTemplateContent就是为了获取微软磁贴预制的模板,update其实是即时信息展示,并不是刻意为了磁贴动态的效果。
public void UpdateTile()
{
if (isPin)
{
var updator = Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForSecondaryTile(this.tileId);
updator.Clear();
updator.EnableNotificationQueue(true); var tile310x150f = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileWide310x150Image);
var tile310x150b = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileWide310x150Image); var imageElement = tile310x150f.GetElementsByTagName("image")[];
imageElement.Attributes.GetNamedItem("src").NodeValue = "ms-appdata:///local/Tile310x150f.jpg";
var notification = new Windows.UI.Notifications.TileNotification(tile310x150f);
updator.Update(notification); imageElement = tile310x150b.GetElementsByTagName("image")[];
imageElement.Attributes.GetNamedItem("src").NodeValue = "ms-appdata:///local/Tile310x150b.jpg";
notification = new Windows.UI.Notifications.TileNotification(tile310x150b);
updator.Update(notification);
}
}
wp8.1 创建动态磁贴应用的更多相关文章
- WP8.1 Study18:动态磁贴
一.前言 动态磁贴在WindowsPhone8.1和Windows8.1都是其特色,有人喜欢有人讨厌,不过我觉得还是挺好的,可以让使用者很快知道App内的内容和吸引使用者打开App.下面来学习下怎样添 ...
- 【Win10 UWP】后台任务与动态磁贴
动态磁贴(Live Tile)是WP系统的大亮点之一,一直以来受到广大用户的喜爱.这一讲主要研究如何在UWP应用里通过后台任务添加和使用动态磁贴功能. 从WP7到Win8,再到Win10 UWP,磁贴 ...
- GifShot - 创建动态 GIF 的 JavaScript 库
GifShot 是一个可以创建流媒体,视频或图像的 GIF 动画的 JavaScript 库.该库的客户端特性使其非常便携,易于集成到几乎任何网站.利用最先进的浏览器 API ,包括 WebRTC , ...
- Unity3D 创建动态的立方体图系统
Unity3D 创建动态的立方体图系统 这一篇主要是利用上一篇的Shader,通过脚本来完成一个动态的立方体图变化系统. 准备工作如下: 创建一个新的场景.一个球体.提供给场景一个平行光,准备2个立方 ...
- Ribbon2: 创建动态的Ribbon库
Sam Radakovitz曾在Excel团队博客中发表过一篇文章,介绍了如何创建动态的Ribbon库,即如何通过RibbonX和VBA放置动态的图形图像到功能区库中,在该文中,作者创建了两个库:一个 ...
- Win8.1应用开发之动态磁贴
using demo02.Common; using System; using System.Collections.Generic; using System.IO; using System.L ...
- 【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI
原文:[ASP.NET Web API教程]2.3.5 用Knockout.js创建动态UI 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容 ...
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...
- 使用Eclipse创建动态的web工程
使用Eclipse创建动态的web工程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.修改工作区的编码 1>.点击Window选择Preferences 2>.将默 ...
随机推荐
- 【整理总结】目录 - 代码沉淀 - 常见Nuget包介绍及使用
为了团队项目或者后期代码维护方便,现在项目已经极致推荐使用Nuget管理第三方类库了,所以下面列举的类库,都会在Nuget上获取到.目录排序不分先后,如果查找困难,请使用 ctrl + F 重要提示: ...
- ModuleNotFoundError: No module named '_sqlite3'
ModuleNotFoundError: No module named '_sqlite3' 解决: 1,首先安装 sqlite-devel yum install sqlite-devel 2,重 ...
- WPF DatePicker 默认显示当前时间
两种方法: 1.通过后台赋值: this.datePicker.SelectedDate = DateTime.Now; 2.前台控件的属性直接赋值 xmlns:sys="clr-names ...
- mac下的一个类似“_kbhit()”实现
#include <sys/select.h> #include <termios.h> #include <sys/ioctl.h> int _kbhit() { ...
- auto_ptr 浅析(转)
auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...
- 学习 TCP 三次握手和四次挥手
TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入一点,他们往往都无法作出准确回答. 本篇尝试使用动画来对这个知识点进行讲解,期望读者们可以更加简单地 ...
- CentOS装机必备-基本设置以及缺失文件
主要是虚拟机中安装CentOS每次总会做一些设置,记录下来方便以后. 纯粹基本设置,比如本地SecureCRT可以连接虚拟机中的CentOS. 复杂的非基本设置见:Linux 命令集锦 设置网络 自 ...
- MVC各层的职责
Model(模型):模型代表着核心的业务逻辑和数据(不要理解成Model只是实体类) View(视图):视图应该关注与如何展示数据,而不应该包含任何业务逻辑(业务逻辑应写在Model中) Contro ...
- Python运维开发基础03-语法基础
上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现" ...
- linux 同步IO: sync、fsync与fdatasync
[linux 同步IO: sync.fsync与fdatasync] 传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据 ...