0

我正在为 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(); 但这根本没有帮助。请给点建议

4

1 回答 1

0

我也在 WP8.1 中使用 ToolStack 生成透明动态磁贴,但使用的是 ExtendedImage.WriteToStream() 方法。

于 2014-05-10T07:48:07.727 回答