【问题标题】:Custom tile generates a System.OutOfMemoryException exception in wp8自定义磁贴在 wp8 中生成 System.OutOfMemoryException 异常
【发布时间】:2014-08-07 05:10:14
【问题描述】:

有人知道为什么我在创建自定义磁贴时会收到 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。

我在诺基亚网站上找到了一篇文章Dynamic Live Tile issue WP8 [WriteableBitmap],有同样的问题,但也没有解决方案。

我明天将继续调试这个,删除所有内容并逐步添加每个位,看看我是否能发现任何东西,但如果有人有任何建议或解决方案,如果你能提供帮助,我将不胜感激。

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

谢谢。

【问题讨论】:

    标签: c# .net windows-phone-8


    【解决方案1】:

    WP BackgroundAgents 有一个小的内存上限。没有更新 3 的 WP8 为 20MB,有更新 3 的 WP8 为 25。

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

    【讨论】:

    • 感谢您的反馈。如果我仍然卡住,我稍后会试一试,但现在,我会更新我的答案。
    【解决方案2】:

    像往常一样,微软在其文档中省略了关键信息。

    在将我的磁贴尺寸从 691x336 减小到 336x165 后,它起作用了,这让我开始思考,我认为微软在这篇文章中推荐的尺寸 Flip Tile template for Windows Phone 8 用于 wp8 和 wp8.1 似乎过大了,所以我做了更多的研究,这是当我在 stackoverflow 的一篇文章中找到这个很好的解释时,即

    Windows Phone 8 Startscreen Tile sizes and margins

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

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

    我将我的(已经缩小的)图块重新调整为 430x210,它仍然有效。

    虽然内存上限为 20/25Mb,具体取决于所使用的操作系统和补丁,这可能会导致我在网上阅读的各种文章中出现许多问题,但在我的例子中,我认为更有可能是受磁贴大小不正确的影响,因此会受到额外内存或缺乏内存的影响。

    一旦我开始在我的 IDE 中使用具有更大分辨率的 8.1,我将更新这篇文章,但现在,我必须假设它肯定与我的 tile 的大小有关。

    我一定会确保添加代码以根据所使用的操作系统、补丁和分辨率创建图块。老实说有点痛苦!

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 2021-09-07
      • 2012-08-22
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多