动态LiveTile -添加背景图像
本文关键字:背景 图像 添加 LiveTile 动态 | 更新日期: 2023-09-27 18:13:42
在我的Windows Phone 7应用程序中使用动态磁贴,它工作得很好。
我现在试图创建一个动态的活动磁贴,我不能得到背景图像显示。当使用下面的代码,我只得到一个黑色的瓷砖。我添加的文本显示,但不是背景图像。图片"Build action"设置为"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
的解决方案:
// 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'nWorld";
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();
试试这个-它对我有效:
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);
我认为问题是图像正在异步加载,因此不可用。我成功地使用了以下代码:
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"/>
在这种情况下,图像永远不会被加载,也不会触发任何事件,可能是因为它没有连接到可视树,因此不会被渲染。