Windows phone 7 Windows Phone-使用自定义图像从后台代理更新live互动程序

Windows phone 7 Windows Phone-使用自定义图像从后台代理更新live互动程序,windows-phone-7,windows-phone-8,writeablebitmap,background-agents,background-audio,Windows Phone 7,Windows Phone 8,Writeablebitmap,Background Agents,Background Audio,如果封面是从互联网加载的,我正在尝试将云图像添加到相册封面。我正试图在后台音频代理中实现这一点,我想我几乎成功了。问题是我的瓷砖中有黑色图像。在测试时,我得到的封面图片中有我的云图像,但大多数情况下我得到的是黑色图像(有时是黑色图像中有云) 有人能帮我找到问题吗?谢谢 private void UpdateAppTile() { var apptile = ShellTile.ActiveTiles.First(); if (apptile != null && _p

如果封面是从互联网加载的,我正在尝试将云图像添加到相册封面。我正试图在后台音频代理中实现这一点,我想我几乎成功了。问题是我的瓷砖中有黑色图像。在测试时,我得到的封面图片中有我的云图像,但大多数情况下我得到的是黑色图像(有时是黑色图像中有云)

有人能帮我找到问题吗?谢谢

private void UpdateAppTile()
{
   var apptile = ShellTile.ActiveTiles.First();
   if (apptile != null && _playList != null && _playList.Any())
   {
      var track = _playList[currentTrackNumber];
      var size = 360;
      Uri coverUrl;
      if (track.AlbumArt.OriginalString.StartsWith("http"))
      {
           BitmapImage img = null;
           using (AutoResetEvent are = new AutoResetEvent(false))
           {
               string filename = Path.GetFileNameWithoutExtension(track.AlbumArt.OriginalString);
               var urlToNewCover = String.Format("http://.../{0}/{1}", filename, size);
               coverUrl = new Uri(urlToNewCover, UriKind.Absolute);
               Deployment.Current.Dispatcher.BeginInvoke(() =>
               {
                   img = new BitmapImage(coverUrl);
                   are.Set();
               });
               are.WaitOne();
               var wbmp = CreateTileImageWithCloud(img);
               SaveTileImage(wbmp, "/shared/shellcontent/test.jpg");
               coverUrl = new Uri("isostore:/shared/shellcontent/test.jpg", UriKind.RelativeOrAbsolute);
           }
       }
       else
       {
           var coverId = track.Tag.Split(',')[1];
           var urlToNewCover = String.Format("http://.../{0}/{1}", coverId, size);
           coverUrl = new Uri(urlToNewCover, UriKind.Absolute);
        }

        var appTileData = new FlipTileData
        {
            BackgroundImage = coverUrl,
            WideBackgroundImage = coverUrl,
            ...
        }
        apptile.Update(appTileData);
   }
}

public static BitmapImage LoadBitmap(string iFilename)
{
    Uri imgUri = new Uri(iFilename, UriKind.Relative);
    StreamResourceInfo imageResource = Application.GetResourceStream(imgUri);
    BitmapImage image = new BitmapImage();
    image.SetSource(imageResource.Stream);
    return image;
}

private void SaveTileImage(WriteableBitmap wbmp, string filename)
{
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists(filename))
            store.DeleteFile(filename);
        var stream = store.OpenFile(filename, FileMode.OpenOrCreate);
        wbmp.SaveJpeg(stream, wbmp.PixelWidth, wbmp.PixelHeight, 100, 100);
        stream.Close();
    }
}

private WriteableBitmap CreateTileImageWithCloud(BitmapImage img)
{
    Image image = null;
    WriteableBitmap wbmp = null;
    using (AutoResetEvent are = new AutoResetEvent(false))
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                image = new Image { Source = img };

                Canvas.SetLeft(image, 0);
                Canvas.SetTop(image, 0);

                var cloud = new BitmapImage(new Uri("Assets/Images/Other/Cloud_no.png", UriKind.Relative));
                var cloudImg = new Image { Source = cloud };

                Canvas.SetLeft(cloudImg, 125);
                Canvas.SetTop(cloudImg, 10);

                var canvas = new Canvas
                {
                    Height = 176,
                    Width = 176
                };
                canvas.Children.Add(image);
                canvas.Children.Add(cloudImg);

                wbmp = new WriteableBitmap(176, 176);

                wbmp.Render(canvas, null);
                wbmp.Invalidate();
                are.Set();
            });
        are.WaitOne();
    }
    return wbmp;
}
编辑
我发现了一个很小的模式,这个模式是有效的,而不是无效的。当应用程序运行时,我调用了两次(在TrackReady和SkipNext中),然后我经常使用云获取封面图像。当我只运行后台代理(不运行应用程序)时,我总是得到黑色图像。通常,第一个UpdatePtile调用只是黑色图像,第二个是带有云的黑色图像。那个黑色是默认的画布背景,所以我想在从url加载封面图像时会遇到延迟问题。但我不确定在我的情况下如何使用ImageOpened事件,以及它是否有帮助。

我认为您应该在向画布添加元素之后和渲染画布之前调用Measure and Arrange(与其他UIElement一样):


谢谢,但没用。
canvas.Measure( new Size( Width, Height ) );
canvas.Arrange( new Rect( 0, 0, Width, Height ) );