简体   繁体   中英

How to navigate a string message and a list and retrieve it into another page using WP8 c#

In my windows phone application, I have two pages CreateGroups and GroupOfContacts and I want to navigate a list and string message from CreateGroups into GroupOfContacts page using button like below:

        private void btn_Click(object sender, RoutedEventArgs e)
        {
List<CustomContact> contact = new List<CustomContact>();
            PhoneApplicationService.Current.State["contactlist"] = contact;
            NavigationService.Navigate(new Uri("/GroupOfContacts.xaml?msg=" + buttonName, UriKind.RelativeOrAbsolute));
            NavigationService.Navigate(new Uri("/GroupOfContacts.xaml?contactlist=" + contact, UriKind.RelativeOrAbsolute));
        }

And I getting string message into GroupOfContacts page like below:

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

    string msg = "";
    if (NavigationContext.QueryString.TryGetValue("msg", out msg))
    {
        tblk_GroupName.Text = msg;
    }
    if (PhoneApplicationService.Current.State["contactlist"] != null)
    {
        listcontact = (List<CustomContact>)(PhoneApplicationService.Current.State["listOfContact"]);
    }  
}

But its getting me exception at this line listcontact = (List<CustomContact>)(PhoneApplicationService.Current.State["listOfContact"]);

Excetion is below:

A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll

Additional information: The given key was not present in the dictionary.

Is there any way to resolve it OR kindly suggest how to navigate a string message and a list together and retrieve from other page using WP8 c#. help me waiting for your reply. Thanks.

When you need to pass multiple parameters using NavigationService, what you should do is use the "&". But here you need to pass a list. So this wont suffice.

Try if this helps.

    private void hyplnk_Next_Click(object sender, RoutedEventArgs e)
    {
      NavigationService.Navigate("/GroupOfContacts.xaml?msg=" +buttonName+ "&listOfContact=1", listOfContact); 
    }

and access it on the other page like ::

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

       string msg;
       if (NavigationContext.QueryString.TryGetValue("msg", out msg))
       {
           tblk_GroupName.Text = msg;
       }

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

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


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

Hope it helps :)

You can also define variables in App class (app.xaml.cs) as public static variables and access them from any page by using

App.buttoName

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