嗯,我是Windows Phone上的新手。System.OutOfMemoryException從Windows Phone 8上的XML創建大量UIElements之後
我只是讀了一個XML數據很多,並保持靜態變量。 當我循環顯示從XML獲取的數據時,我創建了1個包含2個文本塊的畫布。
但是XML有近500行信息,所以該應用程序創建500個Canvas和1000個TextBlocks。 當我嘗試在頁面之間移動時,應用程序速度太慢,有時會凍結,當我從該頁面移動到另一個頁面時,內存不會丟棄,幾乎所有方法都會使應用程序崩潰並導致OutOfMemory異常。
我想知道在我導航到另一個頁面時是否有某些操作可以處理應用程序中使用的內存,或者是否有一些方法可以使用較少的內存在同一個XAML中顯示所有這些數據。
OBS:剛剛加載的頁面使用了近130MB的內存,而手機只有150MB的應用程序。
編輯:
private void empilharParticipantes()
{
string nomeTitulo, nomeEmpresa;
int margin = 0;
SolidColorBrush corTexto = new SolidColorBrush();
corTexto.Color = Color.FromArgb(255, 103, 103, 103);
SolidColorBrush corCanvas = new SolidColorBrush();
corCanvas.Color = Color.FromArgb(255, 242, 242, 242);
items = from query in App.xmlParticipantes.Descendants("Participante")
orderby query.Element("title").Value
select new ABPI.Classes.Participantes
{
nome = (string)query.Element("title"),
empresa = (string)query.Element("company"),
email = (string)query.Element("email"),
telefone = (string)query.Element("phone"),
profissao = (string)query.Element("text"),
endereco = (string)query.Element("address")
};
var itemsArray = items.ToArray();
for (int i = 0; i < itemsArray.Count(); i++)
{
Canvas cnv = new Canvas { Name = itemsArray[i].nome, HorizontalAlignment = HorizontalAlignment.Left, Height = 90, Width = 420, Margin = new Thickness(30, margin, 0, 0), Background = corCanvas };
if (itemsArray[i].nome.Length > 30)
{
nomeTitulo = itemsArray[i].nome.Substring(0, 30) + "...";
}
else
{
nomeTitulo = itemsArray[i].nome;
}
if (itemsArray[i].empresa.Length > 35)
{
nomeEmpresa = itemsArray[i].empresa.Substring(0, 35) + "...";
}
else
{
nomeEmpresa = itemsArray[i].empresa;
}
TextBlock titulo = new TextBlock { Text = nomeTitulo, TextWrapping = TextWrapping.Wrap, FontSize = 25, Foreground = corTexto };
Canvas.SetLeft(titulo, 30);
Canvas.SetTop(titulo, 15);
TextBlock empresa = new TextBlock { TextWrapping = TextWrapping.Wrap, Text = nomeEmpresa, FontSize = 20, Foreground = corTexto };
Canvas.SetLeft(empresa, 30);
Canvas.SetTop(empresa, 45);
cnv.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(uriParametro);
cnv.Children.Add(titulo);
cnv.Children.Add(empresa);
content.Children.Add(cnv);
margin = margin + 100;
}
txtbusca2.ItemsSource = lista;
ContentPanel.Height = margin;
content.Height = margin;
oScroll.Content = content;
循環後,我把的ScrollViewer內的帆布(內容)(oScroll),但是當我離開這個頁面,內存沒有設置,所以當我嘗試去到任何其他地方,應用程序崩潰與System.OutOfMemoryException
發佈用於填充該容器的容器DataTemplate。它是一個ListView,Panorama,Pivot等等等等。 – 2014-09-04 04:58:09
好吧,我把每個畫布創建在一個「主」畫布上。之後,我把這個主畫布放在「ContentPanel」網格內的Scrollviewer上。我將編輯我的帖子以顯示代碼。 – user3746092 2014-09-04 16:32:54