I have a App Xamarin.Forms
that consumes two rest
services. The first service, it consumes normally. But the second, I need to pass a parameter, it is not being consumed. I've tried it in many ways and it does not work. I know my code is buggy, but I do not know where, I do not know how to consume the service. The _data
variable that will populate the Grid with the information is not filled in, but null. Below my codes:
My data service that should load the items from the URL:
public async Task<List<ItensLib>> GetItensLibAsync(int idorcamento)
{
try
{
string url = "http://www.inetglobal.com.br/autorizador/api/itens/{idorcamento}";
var response = await client.GetStringAsync(url);
var itenslib = JsonConvert.DeserializeObject<List<ItensLib>>(response);
return itenslib.ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
My call in the builder of my app's xaml.cs class
public MainPageItens(int idorcamento)
{
InitializeComponent();
this.IdOrcamento = idorcamento;
CarregaDados(idorcamento);
....
async void CarregaDados(int idorcamento)
{
_data = await dataService.GetItensLibAsync(idorcamento);
}
My complete builder, with Grid assembly
public MainPageItens(int idorcamento)
{
InitializeComponent();
this.IdOrcamento = idorcamento;
CarregaDados(idorcamento);
// Crete a grid for "title"
Grid grid = CreateGrid();
grid.Children.Add(new Label { Text = "Produto", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 0, 1, 0, 1);
grid.Children.Add(SeparatorV(), 1, 2, 0, 1);
grid.Children.Add(new Label { Text = "Qtde", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 2, 3, 0, 1);
grid.Children.Add(SeparatorV(), 3, 4, 0, 1);
grid.Children.Add(new Label { Text = "Unitário", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 4, 5, 0, 1);
grid.Children.Add(SeparatorV(), 5, 6, 0, 1);
grid.Children.Add(new Label { Text = "Custo", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 6, 7, 0, 1);
grid.Children.Add(SeparatorV(), 7, 8, 0, 1);
grid.Children.Add(new Label { Text = "Custo Dia", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 8, 9, 0, 1);
grid.Children.Add(SeparatorV(), 9, 10, 0, 1);
grid.Children.Add(new Label { Text = "Ult. Vencto", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 10, 11, 0, 1);
grid.Children.Add(SeparatorV(), 11, 12, 0, 1);
grid.Children.Add(new Label { Text = "Total", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 12, 13, 0, 1);
grid.Children.Add(SeparatorV(), 13, 14, 0, 1);
grid.Children.Add(SeparatorH(), 0, 14, 1, 2);
// Create the ListView to visualize my data
ListView lv = new ListView() { HasUnevenRows = true, SeparatorVisibility = SeparatorVisibility.None };
lv.ItemsSource = _data;
lv.ItemTemplate = new DataTemplate(typeof(ListViewTemplateGrid));
StackLayout sl = new StackLayout() { Children = { grid, lv }, Spacing = 0 };
this.Content = sl;
}
This is the declaration of property _data at the beginning of the class:
List<ItensLib> _data { get; set; }
List<ItensLib> _itens ;
DataService dataService;
Here is the error screenshot (does not say anything), as I know _data
is null
, I think that's the problem:
EDIT1IoverwrittentheOnApperingmethodandremovedthecallandbuildoftheGridfromtheconstructorandswitchedtothismethod.AndintheLoadDatamethod,IpassedittoasyncTaskCarregaDados(intidorcamento);
.WhathappensisthecallintheOnApperingofthismethodisgivingthiserror:
The'wait'operatorcanonlybeusedinanasynchronousmethod.
Seehowitwent:
protectedoverridevoidOnAppearing(){base.OnAppearing();awaitCarregaDados(IdOrcamento);**Aquidáerro**//Creteagridfor"title"
Grid grid = CreateGrid();
grid.Children.Add(new Label { Text = "Produto", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 0, 1, 0, 1);
grid.Children.Add(SeparatorV(), 1, 2, 0, 1);
grid.Children.Add(new Label { Text = "Qtde", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 2, 3, 0, 1);
grid.Children.Add(SeparatorV(), 3, 4, 0, 1);
grid.Children.Add(new Label { Text = "Unitário", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 4, 5, 0, 1);
grid.Children.Add(SeparatorV(), 5, 6, 0, 1);
grid.Children.Add(new Label { Text = "Custo", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 6, 7, 0, 1);
grid.Children.Add(SeparatorV(), 7, 8, 0, 1);
grid.Children.Add(new Label { Text = "Custo Dia", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 8, 9, 0, 1);
grid.Children.Add(SeparatorV(), 9, 10, 0, 1);
grid.Children.Add(new Label { Text = "Ult. Vencto", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 10, 11, 0, 1);
grid.Children.Add(SeparatorV(), 11, 12, 0, 1);
grid.Children.Add(new Label { Text = "Total", TextColor = Color.Red, VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center }, 12, 13, 0, 1);
grid.Children.Add(SeparatorV(), 13, 14, 0, 1);
grid.Children.Add(SeparatorH(), 0, 14, 1, 2);
// Create the ListView to visualize my data
ListView lv = new ListView() { HasUnevenRows = true, SeparatorVisibility = SeparatorVisibility.None };
lv.ItemsSource = _data;
lv.ItemTemplate = new DataTemplate(typeof(ListViewTemplateGrid));
StackLayout sl = new StackLayout() { Children = { grid, lv }, Spacing = 0 };
this.Content = sl;
}
And the LoadData method:
async Task CarregaDados(int idorcamento)
{
_data = await dataService.GetItensLibAsync(idorcamento);
}