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?

1

4 Answers 4

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.
0
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('-');
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.

1
  • Praveen, I was always told this was bad design practice.
    – bluerain
    Nov 18, 2019 at 15:40
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.