[FIXED] After dynamically creating fields, via Code Behind, How to retrieve the value passed by the user?

Issue

I have a place reserved for my Xaml where I will create several fields according to a list coming from the ViewModel.

<StackLayout x:Name="hAcumulativas"/>

Going through the list using the function below I have the fields added to the StackLayout pasted on the screen:

foreach (var item in _vm.ListaTipoHora)
{
    if (item.Acumula == "S")
        IncluirFilhosHAcumulativas(item.Descricao, string.Empty);
    if (item.Acumula == "N")
        IncluirFilhosHNaoAcumulativas(item.Descricao, string.Empty);
}  

private void IncluirFilhosHAcumulativas(string label, string entry)
{
    hAcumulativas.Children.Add(new Label
    {
        Text = label,                
        FontAttributes = FontAttributes.Bold,

    });
    hAcumulativas.Children.Add(new Entry
    {
        Text = entry ,
        Placeholder = "Informe Hodômetro/Horímetro",
        HorizontalOptions = LayoutOptions.CenterAndExpand,
        Keyboard = Keyboard.Numeric
    });
}

I have a save button on the screen, which wants to go through the created fields, since when creating it, there was no Binding at any time.

I’m literally stuck at this point, how to retrieve the values ​​entered by the user? I want to assemble a list and return it to the ViewModel.

Solution

You can loop through your stack layout’s children and inspect the values of the entries. Something like:

foreach (var item in hAcumulativas.Children)
{
    if (item is Entry entry)
    {
        var thing = entry.Text;
    }
}

Answered By – Andrew

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published