![](/img/trans.png)
[英]There was an exception of type '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的一篇文章中找到這個出色的解釋時,即
這清楚地說明了圖塊的大小,但不是基於操作系統版本而是基於屏幕分辨率。
在我的測試環境中,我使用的是默認的“仿真器WVGA 512MB”,默認情況下,屏幕大小為480x800,因此,考慮到本文答案中提供的信息,我的圖塊大小應該為430x210。
我將(已經縮小的)磁貼重新調整為430x210,但仍然可以使用。
雖然內存上限為20 / 25Mb(取決於所使用的操作系統和補丁程序),這可能會導致我在網絡上閱讀過的各種文章引起很多問題,但在我看來,我相信它很可能已受到磁貼的影響大小不正確,因此會增加或減少存儲空間。
我將在分辨率更高的IDE中使用8.1時,將更新本文,但現在,我必須假定它肯定與圖塊的大小有關。
我一定會確保添加代碼以根據所使用的os,補丁和分辨率創建磁貼。 最誠實的人有點痛苦!
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.