Pass / Receive List between pages and fill listview C # Windows Phone 8.1

1

I'm creating a small application for windows phone 8.1, in which the user selects as many checkboxes as necessary, and from there the app loops through all the checkboxes to check which ones are selected, and then pick up their value by playing one and sending it to the next form, so that the selected items are displayed in list form, populating a listview control.

Page 1

        List<ClassDados> lista = new List<ClassDados>();
        ClassDados cDados = new ClassDados();

        foreach (CheckBox c in checkboxes)
        {
            if (c.IsChecked == true)
            {                  
                cDados.Pedido = c.Content.ToString();
                lista.Add(cDados);
            }
        }

        Frame.Navigate(typeof(Carrinho), (lista));

My class

class ClassDados
{
    public string Pedido { get; set; }
    public int Valor { get; set; }

Page 2

public sealed partial class Carrinho : Page
{
    List<ClassDados> lista = new List<ClassDados>();

    public Carrinho()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        ClassDados c = e.Parameter as ClassDados;
        Cardapio car = e.Parameter as Cardapio;

    }

My point is: To receive this data from page 1 fill a listview with the respective data, which I can not actually receive is this data. (Detail: I changed it to C # WP a few months ago, and it changes some things from C # winforms to xaml) and for that reason I can no longer work in the old way to receive this data. Thank you in advance.

    
asked by anonymous 13.05.2016 / 07:50

1 answer

2

You are sending List<ClassDados> to page 2, so on page 2 you will receive the same thing, so just change the mode with which you are receiving the variable

Page 1

Frame.Navigate(typeof(Carrinho), lista);

Page 2

List<ClassDados> lista = new List<ClassDados>();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.Parameter is List<ClassDados>) //checa o tipo do e.Parameter
    {
        lista = e.Parameter as List<ClassDados>; //seta o valor recebido para a variável
    }
}
Now, in the variable lista you will have the variables of page 1. To send the data to ListView , just use foreach .

foreach(ClassDados item in lista)
{
     ...
}

If you need to send more than one object to the other page, use an array:

Frame.Navigate(typeof(Carrinho), new object[] { lista, lista2, lista3 });

Then on page 2, just get the array values

object[] objs = e.Parameter as object[];
lista = objs[0];
lista2 = objs[1];
lista3 = objs[2];

I hope I have helped.

    
25.05.2016 / 02:39