我将网格作为图像保存到独立存储中,以便可以在图块上使用。但是,这只适用于在 xaml 中定义 Grid 的情况。在 C# 中创建时,实际的高度和宽度始终为 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();
}
}