后台任务(可写入位图)将在windows phone中终止
本文关键字:windows 将在 phone 终止 位图 后台任务 | 更新日期: 2023-09-27 17:59:10
我正在为windows phone 8.1更新我的应用程序,我想创建一个透明的实时互动程序。我正在使用ToolStack PNG库来创建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();但这一点帮助都没有。请给一些建议
我也使用ToolStack在WP8.1中生成透明的活动瓦片,但使用方法ExtendedImage.WriteToStream().