0

I am trying to pass an array from page1.xaml to page2.xaml.

int[] array = new int[] { 2,4,6,8,10,12,14,15,17,19,21,23,25,27,28,30,32,34,36,38,40,42,43,45,47,49,51,53,55,56,58,60,62,64,66,68,70,71,73,75,77,79,81,83,84,86,88,90,92,94,96,98,99 
        };
NavigationService.Navigate(new Uri("/Page2.xaml?Parameter" + array, UriKind.Relative));

How can i get this array values in page2?

Ahad Siddiqui
  • 331
  • 1
  • 2
  • 13
  • Using your method, you can only query string parameters. Here's what you are looking for: http://stackoverflow.com/a/14009001/1950812 – fillobotto Dec 19 '13 at 18:00

4 Answers4

3

Passing URL parameters through navigation is useful only for a handful of things:

It's great for a live tile, or for indicating the state of the application and so on.

If you have more complex data being passed between pages, especially if the page with that data passed is not a navigation target - you should definitely not pass it like that.

(If you MUST you can encode it as JSON and then urlencode it, but don't).

Use a service, or a repository pattern for such a thing.

I'd:

  • Define a service in its class file for managing that collection's data.
  • In App.xaml.cs , create the service and pass it around with DI to pages.
  • Make requests to the service in both pages, using a saner way of persistence.
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
3
   int[] array = new int[] { 2,4,6,8,10,12,14,15,17,19,21,23,25,27,28,30,32,34,36,38,40,42,43,45,47,49,51,53,55,56,58,60,62,64,66,68,70,71,73,75,77,79,81,83,84,86,88,90,92,94,96,98,99 
            };
you could use join function like this
string str= string.Join("-", array.ToArray());
    NavigationService.Navigate(new Uri("/Page2.xaml?Parameter="+str, UriKind.Relative));

And in destination page get this passed query string using split function like this

str.Split('-');
Pradeep Kesharwani
  • 1,480
  • 1
  • 12
  • 21
1

Instead of passing array as a query string.Use a Global static class for saving values. And just use the object of that class at the another location.And create new object of the class whenever the task is completed.

Praveen
  • 267
  • 1
  • 9
0

you can try this

page.NavigationService.Navigate(new Uri("/Page2.xaml?Parameter="+array, UriKind.Relative));

Destination page:

string parameter = string.Empty;
if (NavigationContext.QueryString.TryGetValue("parameter", out parameter)) {
    this.label.Text = parameter;
}

note that you are creating the uri without '='. please correc.

also please check this question, it is well explained here:

How to pass values (parameters) between XAML pages?

EDIT: the above suggestion might not work for anything other than strings, please take a look at the detailed solution

Passing a complex object to a page while navigating in a WP7 Silverlight application

Community
  • 1
  • 1
gaurav5430
  • 12,934
  • 6
  • 54
  • 111