I posted a similar question to this and no answer so I thought I would scale it down to a small reproducible example.
I have a loop that adds a grid to a StackLayout for each patient (about 70 records). The grid has a column for a picture (the source of which is a url) and a column for the name. I thought it was the pictures that were causing the out of memory condition, but after hours of stepping through the code, I stumbled on what seems to be the culprit. If I wrap the grids in a frame, that seems to be an issue. I am using Xamarin 4.2.1.62.
I really do want a frame around each patient so I can have a border around each one. Anyone have any ideas?
To reproduce, create a page with a single StackLayout:
`<StackLayout VerticalOptions="FillAndExpand" x:Name="StackPatientList"></StackLayout>`
Then, right after your InitializeComponent() call in the page, call this function:
`private void InitPatients()
{
ObservableCollection<Patient> PatientsList = new ObservableCollection<Patient>();
for (int i = 0; i < 100; i++)
{
PatientsList.Add(new Patient() { FirstName = "First " + i.ToString(), LastName = "Last " + i.ToString() });
}
StackLayout oStack = new StackLayout() { };
this.StackPatientList.Children.Clear();
foreach (Patient oPat in PatientsList)
{
Grid oGrid = new Grid();
oGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(100, GridUnitType.Auto) });
oGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
oGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) });
oGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Image oPatImage = new Image() { Source = oPat.Picture, WidthRequest = 100, HeightRequest = 100 };
Label oPatLabel = new Label() { Text = oPat.FullName, FontSize = 15, VerticalTextAlignment = TextAlignment.Center };
oGrid.Children.Add(oPatLabel, 0, 0);
oGrid.Children.Add(oPatImage, 1, 0);
Frame oFrame = new Frame() { OutlineColor = Color.Black, Padding = 5, VerticalOptions = LayoutOptions.FillAndExpand };
oFrame.Content = oGrid;
//this.StackPatientList.Children.Add(oGrid); // Using this works fine
this.StackPatientList.Children.Add(oFrame); // This causes an out of memory error
}
}`
This is your Patient class:
public class Patient
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName { get { return LastName + ", " + FirstName; } }
public string Picture { get; set; }
}
Run it on an android device (I use an LG-G3) and you will get an out of memory error. Then comment out the line that adds the frame and un-comment the line that just adds the grid and it will work fine.