动态 LiveTile - 添加背景图像? [英] Dynamic LiveTile - adding background image?

查看:17
本文介绍了动态 LiveTile - 添加背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Windows Phone 7 应用程序中使用动态磁贴,它工作得很好.

Working wth live tiles in my Windows Phone 7 app and it's working quite okay.

我现在正在尝试创建动态动态磁贴,但无法显示背景图像.使用下面的代码时,我只会得到一个黑色的瓷砖.显示了我添加的文本,但不显示背景图像.图像构建操作"设置为内容".

I'm now trying to create a dynamic live tile and I can't get the background image to show up. When using the code below I only get a black tile. The text I add is shown but not the background image. Image "Build action" is set to "Content".

有什么想法吗?

StackPanel sp = new StackPanel();
sp.Height = 173;
sp.Width = 173;

string fileName = "tile.jpg";
BitmapImage image = new BitmapImage(new Uri(fileName, UriKind.Relative));
ImageBrush brush = new ImageBrush();
brush.ImageSource = image;
sp.Background = brush;

sp.Measure(new Size(173, 173));
sp.Arrange(new Rect(0, 0, 173, 173));
sp.UpdateLayout();
WriteableBitmap wbm = new WriteableBitmap(173, 173);
wbm.Render(sp, null);
wbm.Invalidate();

推荐答案

我在使用 BitmapImage 时也遇到了问题,但仍然不知道如何解决.但我找到了一个使用 WriteableBitmap 的解决方法:

I also had problems using BitmapImage and still don't know how to solve it. But I found a workaround using WriteableBitmap:

        // grid is container for image and text
        Grid grid = new Grid();

        // load your image
        StreamResourceInfo info = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
        // create source bitmap for Image control (image is assumed to be alread 173x173)
        WriteableBitmap wbmp2 = new WriteableBitmap(1,1);
        wbmp2.SetSource(info.Stream);
        Image img = new Image();
        img.Source = wbmp2;
        // add Image to Grid
        grid.Children.Add(img);

        TextBlock text = new TextBlock() { FontSize = (double)Resources["PhoneFontSizeExtraLarge"], Foreground = new SolidColorBrush(Colors.White) };
        // your text goes here:
        text.Text = "Hello
World";
        grid.Children.Add(text);

        // this is our final image containing custom text and image
        WriteableBitmap wbmp = new WriteableBitmap(173, 173);

        // now render everything - this image can be used as background for tile
        wbmp.Render(grid, null);
        wbmp.Invalidate();

这篇关于动态 LiveTile - 添加背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆