How to transfer an object from a xaml page to another?

Passing a value to another xaml page can be easily done with

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));

But this is only for string values. I would like to pass the object to the xaml page. How to do it?

Found a similar question on the SO and WP7 forum. The solution is to use a global variable (not the nicest solution).

WP7: pass parameter to new page?

http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/81ca8713-809a-4505-8422-000a42c30da8

+3
windows-phone-7 xaml
Nov 29 '10 at 9:06
source share
3 answers

Look at the default code created when you started a new DataBound project. It shows how to pass a link to the selected object to the details page.

+3
Nov 29 '10 at 9:57 on
source share

Using the OnNavigatedFrom Method

OnNavigateFrom is called when we call the NavigationService.Navigate method. It has a NavigationEventArgs object as a parameter, which returns the landing page with the Content property, with which we can access the destination page destination "DestinationPage.xaml.cs"

First, on the DestinationPage.xaml.cs landing page, declare the SomeProperty property:

 public ComplexObject SomeProperty { get; set; } 

Now, in "MainPage.xaml.cs", override the OnNavigatedFrom method:

 protected override void OnNavigatedFrom(NavigationEventArgs e) { // NavigationEventArgs returns destination page "DestinationPage" DestinationPage dPage = e.Content as DestinationPage; if (dPage != null) { // Change property of destination page dPage.SomeProperty = new ComplexObject(); } } 

Now get the value SomeProperty in "DestinationPage.xaml.cs":

 private void DestinationPage_Loaded(object sender, RoutedEventArgs e) { // This will display a the Name of you object (assuming it has a Name property) MessageBox.Show(this.SomeProperty.Name); } 
+5
Oct 29
source share

I recommend watching Caliburn.Micro!

http://caliburnmicro.codeplex.com

+1
Nov 29 '10 at 9:09
source share



All Articles