user1290653
user1290653

Reputation: 775

Passing Own object into navigation Windows Phone 7 (silverlight)

I have my own object and I need to view a list of that object on another windows phone page. This is the code I have so far to navigate to my other page. I need to list the the list of car owners in a listbox on the other page

        private void btnOwners_Click(object sender, RoutedEventArgs e)
        {
             NavigationService.Navigate(new Uri
                    ("/ViewCarMembers.xaml?info=" + 
                      currentCar.Owners, UriKind.Relative));
        }

currentCar.Owners is basically List of that specified car(which of of type Car)

Thank you

Upvotes: 0

Views: 865

Answers (3)

Jamie Keeling
Jamie Keeling

Reputation: 9966

You can't pass an object via the NavigationService.Navigate in that way, it behaves like a URL.

You need to add to the query string the information you need, read it in the OnNavigatedTo function and use the values to generate the data you need:

private void btnOwners_Click(object sender, RoutedEventArgs e) 
{ 
      NavigationService.Navigate(new Uri("/ViewCarMembers.xaml?owners=1,2,3,4,5,6,7,8,9",UriKind.Relative)); 
} 

The in the page you are navigating to:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    var data = this.NavigationContext.QueryString; 

    if (data.ContainsKey("owners"))
    {
       //Iterate through values (comma seperated)
       //Populate a new list with the owners.
    }

    base.OnNavigatedTo(e); 
}

The above code hasn't been tested, it's just something i've made up to get the point across - you could always iterate through your list and append to the query string the full data set, pushing it back into an object on the other side.

For a better example look at how Shai Raiten does it with the following blog post:

http://blogs.microsoft.co.il/blogs/shair/archive/2011/05/01/wp7-data-between-pages-part-1.aspx

Upvotes: 0

Tim Gim
Tim Gim

Reputation: 174

There is another way do pass your object to/from the page. I noticed this trick from wp7 toolkit. DatePicker control works the same. Here is the trick.

You can obtain root frame by using:

var frame = Application.Current.RootVisual as PhoneApplicationFrame

Then you can navigate to another page by using:

frame.Navigate(someUri)

And the key moment is to register your handler for frame.Navigated:

frame.Navigated += OnFrameNavigated;

And then you can pass any value to your navigated page like that:

private void OnFrameNavigated(object sender, NavigationEventArgs e)
    {
        var myPage = e.Content as IMyPage;
        myPage.MyObject = new MyObject();
    }

It's simple!

Upvotes: 1

Praetorian
Praetorian

Reputation: 109289

One way to make this work would be to create a ToCSV() function that takes a List<String> and converts it to a single string where the values in the list are separated by commas. You could then call it as follows:

private void btnOwners_Click(object sender, RoutedEventArgs e)
{
  NavigationService.Navigate(new Uri
    ("/ViewCarMembers.xaml?info=" + 
      ToCSV( currentCar.Owners ), UriKind.Relative));
}

The ToCSV() function will need to call Uri.EscapeDataString on every owner name before apeending it to the output string to insert the appropriate escape characters; the string is automatically unescaped, so don't need to do anything within the ViewCarMembers page. You'll also need to create a function to parse the CSV list and use it on the destination page.

A better solution would be to pass the name of the specified car instead of the names of all its owners, then have the destination page fetch the list of owners from the ViewModel.

Upvotes: 0

Related Questions