Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 - c# - silverlight - Passing XML data between pages

Well, after one week of watching and reading tutorials I still couldn't manage to pass xml data between pages. I had zero knowledge of c# at the beginning, i can understand a bit more know.

I would appreciate if you could guide me or at least tell me where to start. Ok, here is What I want to do and I couldn't

For example; I have an xml data like this;

<document>
    <car id="01">
        <manufacturer>Ford</manufacturer>
        <model>Mustang</model>
        <year>1965</year>
        <details>The Ford Mustang of 1965 was first unveiled bla bla for example....</details>
    </car>
    <car id="02">
        <manufacturer>Chevrolet</manufacturer>
        <model>Nova</model>
        <year>1967</year>
        <details>The Chevrolet Nova of 1967 was bla bla for example....</details>
    </car>
    <car id="03">.....
</document>

I want to show manufacturer and model inside the listbox the mainpage.xaml (It works ok)

TextBlock Text="{Binding manufacturer}"
TextBlock Text="{Binding model}"

When clicked on the model name i want to show year and details of the model in another page (details.xaml for example)

I was able to pass simple text with the method below but I couldn't pass bindings and It didnt work for me. It just list details and year for all the cars not the specific one that i choose in the mainpage.

this.NavigationService.Navigate(
new Uri("/details.xaml?......

Thank you in advance. Sorry if I bothered you.

like image 414
nacon Avatar asked May 24 '26 07:05

nacon


1 Answers

Instead of passing a long string of XML data between pages I'd just pass the car's ID. Since you're data binding to the ListBox on mainpage.xaml I'm assuming you have a class similar to the following:

public Class Car {
  public string manufacturer;
  public string model;
  public unsigned int id;

  // ...
}

Read the XML file on startup and create an ObservableCollection of Car objects which is bound to the ListBox. Then, in the Tap gesture handler for the ListBoxItem do the following:

private void OnListBoxItemTapped( object sender, GestureEventArgs e )
{
  var car = sender.DataContext as Car;

  if( car != null ) {
    // Navigate to page that will display the car details
    NavigationService.Navigate(
          new Uri( String.Format( "/detailspage.xaml?id={0}",
            Uri.EscapeDataString( car.id ) ), UriKind.Relative ) );
  }
}

Then, in the details page OnNavigatedTo method, simply find the correct car using the id and bind that object to the page's DataContext.

Note that EscapeDataString is not necessary if your IDs are always numeric. If you really want to pass all the XML data as is, you can do that as well by replacing the ID with the XML string data.

like image 59
Praetorian Avatar answered May 27 '26 05:05

Praetorian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!