2012-12-16 47 views
1

我不知道如何從WriteAbleBitmap轉到IconicTileData的url屬性IconImage如何在ScheduledAgent中生成圖像?

這裏是我到目前爲止的代碼:

 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

將文件保存到獨立存儲,然後在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 
       });