1
The code at the moment is like this, I am doing all the visual part in the Behind code even, 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 don’t know how to use the value of each entry ( dynamically generated) to do the calculation, does anyone have any idea what I should do? The code is 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));
}
}
got it, but how would I display this list of Ntries on the screen? since I’m using a stack layout
– Leonardo Osvald de Souza
You can put it in a string
– Amadeu Antunes
@Leonardoosvalddesouza how about so?
– Amadeu Antunes
but how would I use the values? because first the user needs to inform them in the Ntries
– Leonardo Osvald de Souza