-1
I’m taking a course in Xamarin
and I stopped in the following item: I have two view, one to register a description of a food and a calorie value of this food. In the other view appears registered foods, or better should appear.
Hometabbedpage.Cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Xamarin.Forms;
namespace AluraNutricao
{
public class HomeTabbedPage : TabbedPage
{
public HomeTabbedPage()
{
//criar uma lista de refeicoes ("lista ObservableCollection") que armazena o tipo Refeicao
ObservableCollection<Refeicao> refeicoes = new ObservableCollection<Refeicao>();
this.Children.Add(new CadastroRefeicao(refeicoes));
this.Children.Add(new ListaRefeicoes(refeicoes));
}
}
}
Listarefeicoes.xaml.Cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AluraNutricao
{
public partial class ListaRefeicoes : ContentPage
{
public ObservableCollection<Refeicao> Refeicoes { get; private set; }
public ListaRefeicoes(ObservableCollection<Refeicao> refeicoes)
{
Refeicoes = refeicoes;
BindingContext = this;
InitializeComponent();
}
}
}
Lists refeicoes.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AluraNutricao.ListaRefeicoes"
Title="Lista Refeições">
<ContentPage.Content>
<StackLayout Padding ="25">
<ListView ItemsSource="{Binding Refeicoes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="5" Spacing="1">
<Label Text="{Binding Teste}" FontSize="12" />
<StackLayout Orientation="Horizontal">
<Label Text="Calorias" FontSize="10" />
<Label Text="{Binding Calorias}" FontSize="10" />
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Refeicao.Cs
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AluraNutricao.ListaRefeicoes"
Title="Lista Refeições">
<ContentPage.Content>
<StackLayout Padding ="25">
<ListView ItemsSource="{Binding Refeicoes}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="5" Spacing="1">
<Label Text="{Binding Teste}" FontSize="12" />
<StackLayout Orientation="Horizontal">
<Label Text="Calorias" FontSize="10" />
<Label Text="{Binding Calorias}" FontSize="10" />
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Criminal record.xaml
<Label Text="Cadastro de refeição" FontSize="24" />
<Entry x:Name="entDescricao" Placeholder="ex: File de frango" />
<StackLayout Orientation="Horizontal">
<Label Text="Calorias" />
<Label Text="" x:Name="lblCalorias" />
</StackLayout>
<Slider Minimum="0" Maximum="1000" x:Name="sldCalorias"
ValueChanged="UpdateCalorias"/>
<Button Text="Cadastrar" Clicked="SalvaRefeicao" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
Cadastrorefeicao.xaml.Cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace AluraNutricao
{
public partial class CadastroRefeicao : ContentPage
{
public ObservableCollection<Refeicao> refeicoes { get; set; }
public CadastroRefeicao(ObservableCollection<Refeicao> refeicoes)
{
refeicoes = new ObservableCollection<Refeicao>();
this.refeicoes = refeicoes;
InitializeComponent();
}
//public CadastroRefeicao(ObservableCollection<Refeicao> refeicoes)
//{
// this.refeicoes = refeicoes;
// }
public void UpdateCalorias(Object sender, EventArgs e)
{
//pega valor do stepper
double valor = sldCalorias.Value;
lblCalorias.Text = valor.ToString();
}
public void SalvaRefeicao(Object sender, EventArgs e)
{
string descricao = entDescricao.Text;
double valor = sldCalorias.Value;
string calorias = sldCalorias.Value.ToString();
if (descricao == "" || calorias=="0"){
string msg = "Por favor, entrar com dados!";
DisplayAlert("Cadastro de refeição", msg, "Ok");
}
else
{
Refeicao refeicao = new Refeicao(descricao, valor);
refeicoes.Add(refeicao);
string msg = "A refeição " + descricao + " de " + calorias + " calorias foi salva com sucesso";
DisplayAlert("Cadastro de refeição", msg, "Ok");
Clear();
}
}
private void Clear()
{
entDescricao.Text = "";
sldCalorias.Value = 0;
}
}
}
App.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
namespace AluraNutricao
{
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new HomeTabbedPage();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
Thank you @Wilker
– Ronaldo.G
Be more specific, put only the code that is related to your problem.
– rubStackOverflow