The code at the moment is like this, I'm doing the whole visual part in the same code behind, without implementing anything directly in XAML. I create the number of entries according to the value entered in an Entry called: sample. As I'm starting in Xamarin / C #, I do not know how to use the value of each entry (generated dynamically) to do the calculation, does anyone have any idea what I should do? The code looks like this:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Leitura : ContentPage
{
Calculo calculo = new Calculo();
public Action<object, EventArgs> Clicked { get; }
public Leitura()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
Entry amostra = new Entry()
{
Placeholder = "Quantidade de amostras",
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.Start,
Keyboard = Keyboard.Numeric,
HeightRequest = 40,
WidthRequest = 230,
};
var layout = new StackLayout();
Label label = new Label()
{
Text = "Amostras",
};
layout.Children.Add(label);
layout.Children.Add(amostra);
Content = layout;
amostra.Completed += Amostra_Completed;
void Amostra_Completed(object sender, EventArgs e)
{
amostra.IsEnabled = false;
calculo.Qnt_amostra = Convert.ToDouble(amostra.Text);
if (calculo.Qnt_amostra > 100)
{
DisplayAlert("Valor Inválido!", "Insira no máximo 100 amostras.", "Ok");
amostra.IsEnabled = true;
}
else
{
if (calculo.Qnt_amostra < 0)
{
DisplayAlert("Valor Inválido!", "Valor não pode ser negativo. ", "Ok");
amostra.IsEnabled = true;
}
else
{
for (int i = 0; i < (int)calculo.Qnt_amostra; i++)
{
//gostaria de utilizar este valor destas entries,(geradas dinamicamente de acordo com a entrada do usuario),para realizar o calculo
var entInput = new Entry();
entInput.IsEnabled = true;
entInput.Keyboard = Keyboard.Numeric;
entInput.Placeholder = $"Peso da {(i + 1)}º amostra";
entInput.WidthRequest = 200;
calculo.Peso = entInput.Text;
layout.Children.Add(entInput);
}
Button botao = new Button
{
Text = "Calcular",
TextColor = Color.White,
BackgroundColor = Color.Green,
WidthRequest = 130,
HeightRequest = 40,
CornerRadius = 5,
};
botao.Clicked += Botao;
layout.Children.Add(botao);
ScrollView scroll = new ScrollView()
{
Content = layout
};
Content = scroll;
}
}
}
}
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var acao = await DisplayAlert("Atenção!", "Todos as medidas serão perdidas! Deseja Contiunar?", "Sim", "Cancelar");
if (acao) await Navigation.PopAsync();
});
return true;
}
public void Botao(object sender, EventArgs e)
{
Navigation.PushAsync(new Resultado(calculo));
}
}