动态 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)
剑心龙吟2024-12-20 20:29:56
试试这个 - 它对我有用:
Uri uri = new Uri("tile.jpg", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
WriteableBitmap wbm = new WriteableBitmap(173, 173);
wbm.SetSource(sri.Stream);
using (var stream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile("/Shared/ShellContent/tile.png"))
{
wbm.SaveJpeg(stream, 173, 173, 0, 100);
}
var data = new StandardTileData();
data.BackgroundImage = new Uri("isostore:/Shared/ShellContent/tile.png", UriKind.Absolute);
data.Title = "updated image";
var tile = ShellTile.ActiveTiles.First();
tile.Update(data);
呆萌少年2024-12-20 20:29:56
我认为问题是图像是异步加载的,因此不可用。
我成功地使用了这段代码:
BitmapImage bmi = new BitmapImage();
bmi.CreateOptions = BitmapCreateOptions.None;
StreamResourceInfo streamInfo =
Framework.App.GetResourceStream(new Uri(@"images\img.png",
UriKind.Relative));
bmi.SetSource(streamInfo.Stream);
imgctl.Source = bmi;
但是,我仍然试图让它在从 Xaml 加载时工作,因此:
<Image HorizontalAlignment="Center" Width="173" Height="89" x:Name="imgctl"
Source="/images/lowsunrise.png"/>
在这种情况下,图像永远不会加载,并且不会触发任何事件,可能是因为它没有连接到可视化树,因此不会被渲染。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我在使用 BitmapImage 时也遇到了问题,但仍然不知道如何解决。但我找到了使用 WriteableBitmap 的解决方法:
I also had problems using
BitmapImage
and still don't know how to solve it. But I found a workaround usingWriteableBitmap
: