user88
user88

Reputation: 1164

navigate list from one page to another windows phone c#

In my windows phone application I have a list of contacts like below:

List<CustomContact> listOfContact1 = new List<CustomContact>();

And below is the CustomContact class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using GetContacts.Resources;
using Microsoft.Phone.UserData;

namespace GetContacts
{
    class CustomContact
    {
        private string[] number = new string[5];
        public string Name { get; set; }
        //public string Number { get; set; }
        public string[] Number
        {
            get { return number; }
            set { number = value; }
        }
       // public string Number1 { get; set; }

        public CustomContact()
        {
        }

        //CTOR that takes in a Contact object and extract the two fields we need (can add more fields)
        public CustomContact( Contact contact)
        {
            Name = contact.DisplayName;
            int count = contact.PhoneNumbers.Count();
            for (int i = 0; i < count; i++)
            {
                if (count > 0 && contact.PhoneNumbers.ElementAt(i).PhoneNumber != null && !string.IsNullOrEmpty(contact.PhoneNumbers.ElementAt(i).PhoneNumber))
                {
                    Number[i] = contact.PhoneNumbers.ElementAt(i).PhoneNumber.ToString();
                }
                else
                {
                   Number[i] = "";
                }
            }
            /*var number = contact.PhoneNumbers.FirstOrDefault();
                if (number != null)
                    Number = number.PhoneNumber;
                else
                    Number = "";*/
        }
    }
}

And I want to navigate list listOfContact1 from my current page like below:

private void hyplnk_Next_Click(object sender, RoutedEventArgs e)
{
    NavigationService.Navigate(new Uri("/createGroups.xaml?listOfContact1=" + listOfContact1, UriKind.Relative));
}

And want to retrieve from another page like below:

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            List<CustomContact> listOfContacts = new List<CustomContact>();
            if (NavigationContext.QueryString.TryGetValue("listOfContact1", out listOfContacts ))
            {
                //do anything from list
            }
        }

But I am getting errors below

Error 1: The best overloaded method match for 'System.Collections.Generic.IDictionary<string,string>.TryGetValue(string, out string)' has some invalid arguments at this line NavigationContext.QueryString.TryGetValue("listOfContact1", out listOfContacts

And

Error 2: Argument 2: cannot convert from 'out System.Collections.Generic.List<GetContacts.CustomContact>' to 'out string' at this line `out listOfContacts`

How do I resolve it, I am following this link http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626521%28v=vs.105%29.aspx and Kindly suggest me How to navigate list of contacts from one page to another, waiting for your reply.Thanks.

Upvotes: 0

Views: 226

Answers (3)

Gaurav Deochakke
Gaurav Deochakke

Reputation: 2285

private void hyplnk_Next_Click(object sender, RoutedEventArgs e)
{
  NavigationService.Navigate("/createGroups.xaml?listOfContact1=1", listOfContact1); 
}

The listOfContact1 = 1 is just used to fetch it on the other page.

This is how you add your list to the uri.

and when you successfully navigate to your page createGroups.xaml there in your OnNavigatedTo handler parse it this way :

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
       // Store test data. 
       List<CustomContact> listContacts = new List<CustomContact>(); 


       // Request parameter. The identification of the source page. 
       string parameter = NavigationContext.QueryString["listOfContact1"]; 

       switch (parameter) 
       { 
           case "1": 
               var myParameter = NavigationService.GetLastNavigationData(); 


               if (myParameter != null) 
               { 
                   listContacts = (List<CustomContact>)myParameter; 
               } 
               break; 
       }
}

Hope this helps.

This link will give you additional details :: http://code.msdn.microsoft.com/wpapps/Pass-non-string-parameters-62ea2cc8 Thanks and Cheers.

Upvotes: 1

vITs
vITs

Reputation: 1560

you can at max pass objects using NavigationService.Navigate.

Also by using State as like:

    PhoneApplicationService.Current.State["Contact"] = Contact;    

but here you want to pass list of that object which is not good programming practice.

You can have this collection as public static at Data model layer (layer atleast above UI layer) and then access it in next screen or anywhere in project.Hope this helps.

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222522

As far as i know When you use the standard use of the NavigationService You cannot pass complex type parameter to another page using QueryString, You can try out the solution which is given here,

Passing complex objects

Upvotes: 1

Related Questions