简体   繁体   中英

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

I'm creating a little app to Win. Phone 8.1, that the user select a number of checkboxes, then, the app do a Foreach to verify wich checkbox is selected, then the app get the checkboxes content (text), fill a and send the list to another page, and in the page 2, fill 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;

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

}

}

My point is: receive the data of page 1 and fill listview/richtexbox control with the data of page 1, but i cant do this, because my way to do this is the same on C# Windows Forms, but is different to windows phone, anyone can help me?

I'm not sure I completely understand your question. Do you want to pass the List<ClassDados> created on Page 1 to Page 2?

Firstly, move the ClassDados cDados = new ClassDados(); line into the foreach loop (Page 1):

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

Then add another constructor to Page 2:

public Carrinho(List<ClassDados> cDados)
{
    this.InitializeComponent();
    // Use cDados to populate your control
}

I also notice that ClassDados.Valor is unused. You could add a constructor to ClassDados that accepts a string and populates the Pedido property.

The problem is that you want to receive an object of type ClassDados on Page 2 , although on Page 1 you pass a List of ClassDados List<ClassDados> lista . So on Page 2 write List<ClassDados> lista = e.Parameter as List<ClassDados> . That should do the trick. Also, make sure you check for null when retrieving object from e.Parameter !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM