3

Passing value to another xaml page can be done easily with

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

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

Found a similar question on 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

Community
  • 1
  • 1
samwize
  • 25,675
  • 15
  • 141
  • 186

3 Answers3

5

Using OnNavigatedFrom method

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

First, in the destination page "DestinationPage.xaml.cs", declare a property "SomeProperty":

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 SomeProperty value 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);
}
Luxspes
  • 6,268
  • 2
  • 28
  • 31
  • While this approach may work to pass a reference to a destination page, it will not work to receive the same reference in the source page when the destination release control back to the source. There is no guarantee that the destination page's reference is valid after control is returned to the source page. In general, the only real solution is to have a shared object reference stored in the current application object (Application.Current), to which both the source and destination pages have access and which is guaranteed to always remain valid during the lifetime of the app instance. – Mark Jones Jun 04 '13 at 19:34
  • My answer is valid for the question as it is stated. I'd recommend you to transform your scenario in to a question (with code), and I'll try to answer it without using an ugly global in Application.Current (so far I have always been able to avoid them) – Luxspes Jun 06 '13 at 13:28
3

Have a look at the default code created when you start a new DataBound Project. It shows a way of passing a reference to a selected object to a details page.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • Thanks. The default project pass the object indirectly via query string, which then access from `App.ViewModel`. That will work, but I was hoping to have a more elegant solution of passing the object directly. – samwize Nov 30 '10 at 03:08
  • i think there is no elegant solution, so got to do this indirect way. otherwise, use a framework. – samwize Dec 14 '10 at 05:24
  • There is an elegant solution, please look at my answer – Luxspes Mar 23 '13 at 23:44
1

I recommend looking at Caliburn.Micro!

http://caliburnmicro.codeplex.com

  • Thanks I will look into this framework (have heard it couple of times). Do you know if it can pass object to pages? – samwize Nov 30 '10 at 02:59
  • Binding to the ViewModel/Presenter/Controller/Screen behind should probably manage this! –  Nov 30 '10 at 06:20