C# 后台任务(可写位图)将在windows phone中终止

C# 后台任务(可写位图)将在windows phone中终止,c#,memory-leaks,windows-phone,png,writablebitmap,C#,Memory Leaks,Windows Phone,Png,Writablebitmap,我正在更新适用于windows phone 8.1的应用程序,我想创建一个透明的live互动程序。我正在使用创建png图像,它在第一眼就可以很好地工作! 我正在后台任务中使用下面的代码 private void CreateWideTile() { int width = 691; int height = 336; string imagename = "WideBackground"; WriteableBitmap

我正在更新适用于windows phone 8.1的应用程序,我想创建一个透明的live互动程序。我正在使用创建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