Search code examples
c#windows-phone-8windows-phone-8.1

Pass a list between screens. Windows Phone 8.1 C#


I'm using Visual Studio 15 and started creating an app for windows phone 8.1 So at this point i need to check all checked checkboxes and pick his text and add checked values to a List (This already did) now, i need to pass this list between pages, i'm stuck, i know only winforms way, and it's not work

Here de the code of page 1

        CheckBox[] checkboxes = new CheckBox[] { checkBox, checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7,checkBox8,checkBox9, checkbox10, checkbox11, checkbox12, checkbox13};

        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), (cDados));

Now the code on second page

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

    public <here>Carrinho</here>(List<ClassDados> cDados)
    {
        this.InitializeComponent();
        lista = cDados;
    }...

My Class

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

"Here" on second page im getting the error: Error CS0051 Inconsistent accessibility: parameter type 'List' is less accessible than method 'Carrinho.Carrinho(List)'

On windows forms C# i've used this way so much, but on WP it's not working, so can anyone tell me the correct way to do this? Thanks.


Solution

  • Frame.Navigate(typeof(dest_page), parameter);
    

    Just serializes parameter and pass it along to your dest_page, it doesn't call the constructor like you are assuming.

    To get parameter you want to override the OnNavigatedTo(NavigationEventArgs e) of the second page like so:

    public sealed partial class Carrinho : Page
    {
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            YOUR_CLASS c = e.Parameter as YOUR_CLASS;
        }
    }
    

    Make sure the object you're passing is serializable (just keep it simple, and it will be okay).


    Another way you can do this is to have a Global Resource Locator. You can search SO for how to do this. So basically, it's just a resource and you can reference it on any page you like.