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

Asked

Viewed 259 times

1

I am creating a small application for windows phone 8.1, in which the user selects as many checkboxes as necessary, and from that the app goes through in a loop all the checkboxes to check which are selected, and so get its value, playing in one and sending to the next form, so that the selected items are displayed in list form, filling 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 in a listview with the respective data, what I can’t actually, is to receive this data. (Detail: I switched to C# WP a few months ago, and changes some things from C# winforms to xaml) and for this reason I can no longer work the old way to receive this data. Thank you from now.

1 answer

2


You are sending a List<ClassDados> to page 2, then 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
    }
}

Ready, now on the variable lista you will have the variables of page 1. To send the data to the ListView, just use a 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 });

From there on page 2, just take the array values

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

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.