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

我们写一个静态方法。在使用RenderTargetBitmap类的同时,建议去看一下微软的关于此类的用法,参数中的element元素也有所限制。

1.一些视频无法生成图像。

2.自定义一些第三方插件无法生成图像。

3.元素visibility必须要为visible。

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);
}
}

最新文章

  1. bzoj1510: [POI2006]Kra-The Disks(单调栈)
  2. 通过grep来进行日志分析,grep -C和配合awk实际对catalina.out使用案例
  3. 获取当前运行dll文件的路径
  4. jquery客户端验证插件
  5. Android 图片的裁剪与相机调用
  6. struts2 if标签示例
  7. MSSQL 2005数据库与SP4补丁安装
  8. Java汉字排序(1)排序前要了解的知识(数组和list的排序接口)
  9. [liu yanling]软件测试分为哪几个计划过程阶段
  10. mysql中的group_concat函数的用法
  11. phpword的几个坑
  12. 华为Java笔试题
  13. 强烈推荐visual c++ 2012入门经典适合初学者入门
  14. eclipse: eclipse创建java web项目
  15. idea中war和war exploded的区别及修改jsp必须重新启动tomcat才能生效的问题
  16. for in和for of的区别(转)
  17. 继承 派生 super()经典类 新式类
  18. Palindromic characteristics CodeForces - 835D (区间DP,预处理回文串问题)
  19. MongoDB下Map-Reduce使用简单翻译及示例
  20. linux下vi或vim操作Found a swap file by the name的原因及解决方法

热门文章

  1. WebAPI认证与授权
  2. win10 svn commit无响应
  3. python 阿里云短信群发推送
  4. bootstrap更新数据层
  5. 可视化库-Matplotlib-散点图(第四天)
  6. MVC4 AspNet MVC下的Ajax / 使用微软提供的Ajax请求脚本 [jquery.unobtrusive-ajax.min.js]
  7. 【转】mac os、linux及unix之间的关系
  8. Linux系统下使用ckfinder上传中文名的图片无法正常预览
  9. windchill 跑物料变更流程后无法发送物料到SAP
  10. PLSQL Developer工具的使用