Sto creando e applicazione che deve aggiungere e rimuoverlo molto UIElement
a a Canvas
. Fondamentalmente a Canvas
contiene una raccolta di UIElement
e lo rende/aggiorna automaticamente sullo schermo a seconda di ciò che contiene.
Per evitare di avere un sacco di UIElements
che si sovrappongono a vicenda sullo schermo, preferisco aggiungerli tutti su un secondario Canvas
Quindi crea un Image
da esso (grazie a WritableBitmap
). Finalmente lo aggiungo Image
sulla mia corrente Canvas
. Permettendo di avere solo poche immagini sulla mia tela, mi aspetto di avere prestazioni migliori. Purtroppo sembra che non posso eliminare completamente il WritableBitmap
, anche se lo imposto null
.
Il seguente codice lo illustra:
//My constructor
public WP8Graphics()
{
//Here my collection DataBinded with the Canvas from the Mainpage
this.UIElements = new ObservableCollection<UIElement>();
//The secondary Canvas
GraphicCanvas = new Canvas();
GraphicCanvas.Height = MainPage.CurrentCanvasHeight;
GraphicCanvas.Width = MainPage.CurrentCanvasWidth;
}
///This method can be hit thousand times, it basically create a rectangle
public void fillRect(int x, int y, int width, int height)
{
// some code
// CREATE THE RECTANGLE rect
GraphicCanvas.Children.Add(rect); // My secondary Canvas
WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);
wb1.Invalidate();
WriteableBitmap wb2 = new WriteableBitmap((int)MainPage.CurrentCanvasWidth, (int)MainPage.CurrentCanvasHeight);
for (int i = 0; i < wb2.Pixels.Length; i++)
{
wb2.Pixels[i] = wb1.Pixels[i];
}
wb2.Invalidate();
wb1 = null;
Image thumbnail = new Image();
thumbnail.Height = MainPage.CurrentCanvasHeight;
thumbnail.Width = MainPage.CurrentCanvasWidth;
thumbnail.Source = wb2;
this.UIElements.Add(thumbnail);
}
Dopo qualcosa come 24 WriteableBitmap
creato a OutfmemoryException appare. Ho letto molti articoli su questo problema e nel mio caso sembra il WriteableBitmap
Dipende dal mio GraphicCanvas e rimane perché ci sono ancora un riferimento ad esso. Non riesco a eliminare la mia tela grafica né set myimage Fonte a NULL.
Ho 2 domande:
- Esiste un altro modo per creare un'immagine da tela o una raccolta di uielements?
- È possibile rimuovere il riferimento chi mantiene viva quella scritta?
Spero di essere abbastanza chiaro e facile da leggere.
Grazie per aver letto.
A cura di Atomaras Suggerction, ma ancora lo stesso problema
WriteableBitmap wb1 = new WriteableBitmap(GraphicCanvas, null);
Questa linea lancia ancora outofmemoryException.