繁体   English   中英

自定义磁贴在wp8中生成System.OutOfMemoryException异常

[英]Custom tile generates a System.OutOfMemoryException exception in wp8

有谁知道为什么在创建自定义图块时会出现OutOfMemoryException?

我正在尝试从ScheduledAgent为Windows Phone 8应用上的主图块创建自定义图像。 在执行我的最后一行代码NotifyComplete()之前,不会发生该错误。

这是代码(我猜不是最干净的,但可以进行原型制作)。 该代码仅处理宽块,并尝试加载从网站下载的图像,然后尝试在该图像上呈现徽标和说明。

这是代码:

    private void CreateTiles()
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            for (int i = 0; i < 2; i++)
            {
                var bmp = new WriteableBitmap(691, 336);
                var articleImg = new BitmapImage(new Uri(articles[i].ImageFilename, UriKind.Relative));
                var articleImage = new Image { Source = articleImg };
                articleImage.Stretch = Stretch.UniformToFill;
                articleImg.CreateOptions = BitmapCreateOptions.None; // Force the bitmapimage to load it's properties so the transform will work

                var bmpLogo = new WriteableBitmap(100, 100);
                var logoImg = new BitmapImage(new Uri("/Assets/Tiles/FlipCycleTileSmall.png", UriKind.Relative));
                var logoImage = new Image { Source = logoImg };
                logoImage.Opacity = 1.0;
                logoImg.CreateOptions = BitmapCreateOptions.None; // Force the bitmapimage to load it's properties so the transform will work

                var articleBannerGrid = new Grid();
                articleBannerGrid.Background = ColorExtensions.ToSolidColorBrush("#000F558E");
                articleBannerGrid.Opacity = .5;
                articleBannerGrid.Height = 100;
                articleBannerGrid.VerticalAlignment = VerticalAlignment.Bottom;
                articleBannerGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
                articleBannerGrid.ColumnDefinitions.Add(new ColumnDefinition());
                articleBannerGrid.Children.Add(logoImage);

                Grid.SetColumn(logoImage, 0);

                var textBlock = new TextBlock();
                textBlock.Text = articles[i].Description;
                textBlock.FontWeight = FontWeights.Bold;
                textBlock.Margin = new Thickness(10, 5, 30, 5);
                textBlock.TextWrapping = TextWrapping.Wrap;
                textBlock.Foreground = new SolidColorBrush(Colors.White); //color of the text on the Tile
                textBlock.FontSize = 30;
                textBlock.Opacity = 1.0;

                var articleTextGrid = new Grid();
                articleTextGrid.Height = 100;
                articleTextGrid.VerticalAlignment = VerticalAlignment.Bottom;
                articleTextGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100) });
                articleTextGrid.ColumnDefinitions.Add(new ColumnDefinition());
                articleTextGrid.Children.Add(textBlock);

                Grid.SetColumn(textBlock, 1);

                var canvas = new Grid();
                canvas.Width = articleImg.PixelWidth;
                canvas.Height = articleImg.PixelHeight;
                canvas.Children.Add(articleImage);
                canvas.Children.Add(articleBannerGrid);
                canvas.Children.Add(articleTextGrid);

                bmp.Render(canvas, null);
                bmp.Invalidate(); //Draw bitmap

                FileStream fs = new FileStream(articles[i].ImageFilename, FileMode.Create);
                bmp.SaveJpeg(fs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                fs.Close();
                fs.Dispose();
                articleImage = null;
                articleImg = null;
            }

            //GC.Collect();
            //GC.WaitForPendingFinalizers();

            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault();

            if (TileToFind != null)
            {
                string title = articles[0].Tag;
                string backTitle = articles[1].Tag;
                string content = articles[0].Description;
                string backContent = articles[1].Description;

                FlipTileData tileData = new FlipTileData()
                {
                    Title = title,
                    BackTitle = backTitle,
                    BackContent = backContent,
                    WideBackContent = backContent,
                    BackgroundImage = new Uri(articles[0].ImageFilename, UriKind.Relative),
                    BackBackgroundImage = new Uri(articles[1].ImageFilename, UriKind.Relative),
                    WideBackgroundImage = new Uri(articles[0].ImageFilename, UriKind.Relative),
                    WideBackBackgroundImage = new Uri(articles[1].ImageFilename, UriKind.Relative),
                };

                TileToFind.Update(tileData);
            }

            NotifyComplete();

        });
    }

我认为这是生成自定义图块(即ScheduledAgent)的正确位置。

我在诺基亚网站上发现了文章动态动态平铺问题WP8 [WriteableBitmap] ,该问题同样存在,但也没有解决方法。

明天,我将继续进行调试,删除所有内容并逐步添加每个位,以查看是否可以找到任何东西,但是如果有人有任何建议或解决方案,我将不胜感激。

如果我使用错误的方法,请告诉我更新计划的代理中磁贴的最佳方法是什么。

谢谢。

WP BackgroundAgents的存储容量很小。 WP8(不带更新3)为20MB,WP8(带更新3)为25MB。

我建议您创建另一个项目,并将Coding4Fun工具包和MemoryCounter添加到您的页面中,然后将BackgroudAgent的代码添加到示例页面中,并尝试创建图块。 然后查看它使用了多少内存。 我认为内存使用量高于20 / 25MB上限。 如果是这样,您必须找到减少它的方法。

与往常一样,Microsoft从其文档中省略了关键信息。

将图块的尺寸从691x336减小到336x165后,它可以正常工作,因此让我开始思考,我认为Microsoft在本文针对Windows Phone 8的wp8和wp8.1的翻转图块模板中推荐的尺寸似乎过大,所以我做了更多的研究,这是当我在stackoverflow的一篇文章中找到这个出色的解释时,即

Windows Phone 8“开始屏幕”图块大小和边距

这清楚地说明了图块的大小,但不是基于操作系统版本而是基于屏幕分辨率。

在我的测试环境中,我使用的是默认的“仿真器WVGA 512MB”,默认情况下,屏幕大小为480x800,因此,考虑到本文答案中提供的信息,我的图块大小应该为430x210。

我将(已经缩小的)磁贴重新调整为430x210,但仍然可以使用。

虽然内存上限为20 / 25Mb(取决于所使用的操作系统和补丁程序),这可能会导致我在网络上阅读过的各种文章引起很多问题,但在我看来,我相信它很可能已受到磁贴的影响大小不正确,因此会增加或减少存储空间。

我将在分辨率更高的IDE中使用8.1时,将更新本文,但现在,我必须假定它肯定与图块的大小有关。

我一定会确保添加代码以根据所使用的os,补丁和分辨率创建磁贴。 最诚实的人有点痛苦!

希望这可以帮助。

暂无
暂无

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

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