动态 LiveTile - 添加背景图像?
在我的 Windows Phone 7 应用程序中使用动态磁贴,效果非常好。
我现在正在尝试创建动态动态图块,但无法显示背景图像。当使用下面的代码时,我只得到一个黑色的瓷砖。显示我添加的文本,但不显示背景图像。图像“构建操作”设置为“内容”。
有什么想法吗?
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();
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".
Any ideas?
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在使用 BitmapImage 时也遇到了问题,但仍然不知道如何解决。但我找到了使用 WriteableBitmap 的解决方法:
I also had problems using
BitmapImage
and still don't know how to solve it. But I found a workaround usingWriteableBitmap
:试试这个 - 它对我有用:
Try this - it worked for me:
我认为问题是图像是异步加载的,因此不可用。
我成功地使用了这段代码:
但是,我仍然试图让它在从 Xaml 加载时工作,因此:
在这种情况下,图像永远不会加载,并且不会触发任何事件,可能是因为它没有连接到可视化树,因此不会被渲染。
I think the problem is the image is being loaded asynchronously, and therefore not available.
I succeeded with this code:
However, I'm still trying to get it to work when loaded from Xaml thus:
In that case, the image is never loaded, and none of the events fire, probably because it's not connected to the visual tree, and therefore won't be rendered.