タイルで使用できるように、グリッドを画像として分離ストレージに保存しています。ただし、これはグリッドが xaml で定義されている場合にのみ機能します。C# で作成した場合、実際の Height と Width は常に 0 です。backgroundtask からこのメソッドを呼び出してタイルを更新したいのですが、そこで xaml を定義することはできません。これを回避する方法はありますか?
public void SaveImageFromGrid(Grid g, string fileName)
{
int w = Convert.ToInt32(g.ActualWidth);
int h = Convert.ToInt32(g.ActualHeight);
WriteableBitmap wb = new WriteableBitmap(w, h);
wb.Render(g, null);
wb.Invalidate();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(fileName))
{
myIsolatedStorage.DeleteFile(fileName);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);
StreamResourceInfo sri = null;
Uri uri = new Uri(fileName, UriKind.Relative);
sri = Application.GetResourceStream(uri);
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
}