windows-phone-7 - Windows Phone - 使用自定义图像从后台代理更新动态磁贴

标签 windows-phone-7 windows-phone-8 writeablebitmap background-agents background-audio

如果封面是从互联网加载的,我正在尝试将云图像添加到专辑封面。我正在尝试在后台音频代理中执行此操作,我想我几乎明白了。问题是我在图 block 中有黑色图像。几次测试时,我得到的封面图像中有我的云图像,但大多数情况下我得到的是黑色图像(有时是黑色图像中有云)。

谁能帮我找出问题所在?谢谢

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 中),然后我经常得到带有云的封面图像。当我只运行后台代理(不运行应用程序)时,我总是得到黑色图像。通常第一个 UpdateAppTile 调用只是黑色图像,第二个是带有云的黑色图像。黑色是默认的 Canvas 背景,所以我想我在从 url 加载封面图像时遇到了延迟问题。但我不确定在我的情况下如何使用 ImageOpened 事件以及它是否有帮助。

最佳答案

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

canvas.Measure( new Size( Width, Height ) );
canvas.Arrange( new Rect( 0, 0, Width, Height ) );

关于windows-phone-7 - Windows Phone - 使用自定义图像从后台代理更新动态磁贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940519/

相关文章:

silverlight - WP7 : ScrollViewer viewport size

c# - 是否可以重载或更改复制和粘贴功能?

windows - 在Windows Phone 8中播放音频时如何录制或获取音频?

windows-phone-7 - 需要时在 TextBlock 中添加新行

.net - 用颜色覆盖WriteableBitmap

仅在 HP Z620 站上发生的 C# Out Of Memory/COMException

xaml - Windows Phone 枢轴显示在应有的位置上方

c# - 在 Windows Phone 运行时更改图像控件的 z 顺序

azure - 没有 Azure 服务的推送通知?

c# - WriteableBitmap 在 Silverlight 4 中有新功能吗?