繁体   English   中英

后台任务(writablebitmap)将在Windows Phone中终止

[英]Background task (writablebitmap) will be terminated in windows phone

我正在为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()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM