C# 动态LiveTile-添加背景图像?

C# 动态LiveTile-添加背景图像?,c#,windows-phone-7,live-tile,C#,Windows Phone 7,Live Tile,在我的WindowsPhone7应用程序中使用live tiles,并且工作正常 我现在正试图创建一个动态的实时互动程序,但我无法显示背景图像。当使用下面的代码时,我只得到一块黑色的瓷砖。将显示我添加的文本,但不显示背景图像。图像“构建操作”设置为“内容” 有什么想法吗 StackPanel sp = new StackPanel(); sp.Height = 173; sp.Width = 173; string fileName = "tile.jpg"; BitmapImage imag

在我的WindowsPhone7应用程序中使用live tiles,并且工作正常

我现在正试图创建一个动态的实时互动程序,但我无法显示背景图像。当使用下面的代码时,我只得到一块黑色的瓷砖。将显示我添加的文本,但不显示背景图像。图像“构建操作”设置为“内容”

有什么想法吗

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

试试这个-它对我有用:

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时也遇到了问题,但仍然不知道如何解决。但是我发现了一个使用
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();

我认为问题在于映像是异步加载的,因此不可用。 我成功地使用了以下代码:

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"/>


在这种情况下,永远不会加载图像,并且不会触发任何事件,这可能是因为图像未连接到视觉树,因此不会被渲染

谢谢克里斯的帮助!我最终测试了下面的另一个解决方案,因为它看起来更像我的代码,我对它更熟悉(我是一个快乐的初学者:),并且努力掌握一切。谢谢Heinrich!很好用!您的代码中是否曾经出现过图像未完全加载因而未显示的情况?我读过,但这可能不是一个问题,对吗?它已经发生了,我怀疑这也是一个问题。有时将
CreateOptions
()设置为
None
有助于立即加载图像,但在这里似乎不起作用。所以我真的很想尝试类似于
BitmapImage.ForceLoadingNow()
的方法,但是我找不到合适的方法。啊哈,好吧,这很糟糕。这经常发生吗?是wbmp2出现故障,还是未显示活动磁贴的整个布局(wbmp)?还有其他人可以帮助我们吗?只需使用
BitmapImage
,就可以了,所以不用担心上面的代码。它故意不使用它。“here as well”的意思是“在使用BitmapImage的原始代码中”,而不是“here over”;)你是说我的原始代码是你注意到问题的地方,但不是在你的代码中?