如何在ScheduledAgent中生成图像?

时间:2012-12-16 10:51:17

标签: c# windows-phone

我不确定如何从WriteAbleBitmap转到IconicTileData的网址IconImage

到目前为止,这是我的代码:

        protected override void OnInvoke(ScheduledTask task)
        {
            ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault();

            if (tile != null)
            {
                WriteableBitmap genTile = renderTile(202, 202);

                tile.Update(new IconicTileData()
                {
                    Title = "IconicTileData",
                    IconImage = /* PATH TO genTile */
                });
            }

            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(3));

            NotifyComplete();
        }

        private WriteableBitmap renderTile(int width, int height)
        {
            Canvas can = new Canvas();
            can.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
            can.Width = width;
            can.Height = height;

            WriteableBitmap tileImage = new WriteableBitmap(width, height);

            tileImage.Render(can, null);

            tileImage.Invalidate();
            return tileImage;
        }

解决方案是保存文件?我怎么能这样做,ShellTile与应用程序不共享相同的空间?

1 个答案:

答案 0 :(得分:1)

将文件保存到独立存储,然后在Uri中使用“isostore:”前缀。

public static void StoreSavedResultImage(string filename, WriteableBitmap wb)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(filename))
                    isf.DeleteFile(filename);

                using (IsolatedStorageFileStream fs = isf.CreateFile(filename))
                {
                    wb.SaveJpeg(fs, wb.PixelWidth, wb.PixelHeight, 0, 100);
                    fs.Close();
                    wb = null;
                    img = null;
                }
            }
        }

如果要在实时磁贴中引用隔离存储中的文件,则该文件应保存在/ Shared / ShellContent文件夹中。

Uri wideUri = new Uri("isostore:/Shared/ShellContent/app_wide.jpg"), UriKind.Absolute);

tile.Update(new IconicTileData()
                {
                    Title = "IconicTileData",
                    IconImage = wideUri
                });