简体   繁体   English

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

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

I'm updating my app for windows phone 8.1 and I want to create a transparent live tile. 我正在为Windows Phone 8.1更新我的应用程序,我想创建一个透明的实时图块。 I'm using The ToolStack PNG library for creating png images and it works fine in first look ! 我正在使用ToolStack PNG库创建png图像, 乍一看效果很好! I am using the code below in background task 我在后台任务中使用下面的代码

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();
        }

    }

as I mentioned, this code works fine but after 5 times run, the background task will be terminated and won't run anymore...I'm really confused. 正如我提到的那样,此代码可以正常工作,但是运行5次后,后台任务将终止并且不再运行...我真的很困惑。 I've searched all the internet and I just guess the problem is because of memory leak. 我已经搜索了所有的互联网,我只是猜测问题出在内存泄漏。 but I don't know how to solve this confusing problem. 但我不知道如何解决这个令人困惑的问题。 I also used GC.Collect(); 我还使用了GC.Collect(); but it didn't help at all. 但这根本没有帮助。 please please give some advice 请给一些建议

我还使用ToolStack在WP8.1中生成透明的实时图块,但使用了ExtendedImage.WriteToStream()方法。

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

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