我正在为Windows Phone 8.1更新我的应用程序,我想创建一个透明的实时图块。我正在使用ToolStack PNG library创建png图像,它在第一眼看上去很好用! 我在后台任务中使用以下代码
private void CreateWideTile()
{
int width = 691;
int height = 336;
string imagename = "WideBackground";
WriteableBitmap b = new WriteableBitmap(width, height);
var canvas = new Grid();
canvas.Width = b.PixelWidth;
canvas.Height = b.PixelHeight;
var textBlock = new TextBlock();
textBlock.Text = "example text...";
textBlock.Margin = new Thickness(10, 55, 0, 0);
textBlock.Width = 140;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.FontSize = 30;
canvas.Children.Add(textBlock);
b.Render(canvas, null);
b.Invalidate();
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
b.WritePNG(stream);
stream.Close();
}
}
private void CreateMediumTile()
{
int width = 336;
int height = 336;
string imagename = "MediumBackground";
WriteableBitmap b = new WriteableBitmap(width, height);
var canvas = new Grid();
canvas.Width = b.PixelWidth;
canvas.Height = b.PixelHeight;
var textBlock = new TextBlock();
textBlock.Text = "example text...";
textBlock.Margin = new Thickness(10, 55, 0, 0);
textBlock.Width = 140;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.FontSize = 30;
canvas.Children.Add(textBlock);
b.Render(canvas, null);
b.Invalidate();
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
b.WritePNG(stream);
stream.Close();
}
}
private void CreateSmallTile()
{
int width = 159;
int height = 159;
string imagename = "SmallBackground";
WriteableBitmap b = new WriteableBitmap(width, height);
var canvas = new Grid();
canvas.Width = b.PixelWidth;
canvas.Height = b.PixelHeight;
var textBlock = new TextBlock();
textBlock.Text = "example text...";
textBlock.Margin = new Thickness(10, 55, 0, 0);
textBlock.Width = 140;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.FontSize = 30;
canvas.Children.Add(textBlock);
b.Render(canvas, null);
b.Invalidate();
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists("/Shared/ShellContent/" + imagename + ".png"))
store.DeleteFile("/Shared/ShellContent/" + imagename + ".png");
var stream = store.OpenFile("/Shared/ShellContent/" + imagename + ".png", FileMode.OpenOrCreate);
b.WritePNG(stream);
stream.Close();
}
}
正如我所提到的,这段代码运行正常但经过5次运行后,后台任务将被终止并且不再运行...我真的很困惑。我搜索了所有的互联网,我只是猜测问题是因为内存泄漏。但我不知道如何解决这个令人困惑的问题。 我也用过GC.Collect();但它根本没有帮助。 请提出一些建议
答案 0 :(得分:0)
我还使用ToolStack在WP8.1中生成透明的实时图块,但使用方法ExtendedImage.WriteToStream()。