How to navigate with objects in windows phone 8?

2019-02-15 20:16发布

问题:

I need to navigate from one xaml page to another with an Object not a String ..

present code is :

  private void Border_ManipulationStarted(object sender,    System.Windows.Input.ManipulationStartedEventArgs e)
{
     string uri = "/PhonePageOne.xaml?Text=";
     uri += txtBox.Text;
     NavigationService.Navigate(new Uri(uri, UriKind.Relative));
}

i dont want to pass a text in the url, i need to pass an object instead of that like below , and any way to do this ?

Person p = new person();
uri+=p

回答1:

In the first page do the following:

PhoneApplicationService.Current.State["param"] = p;
NavigationService.Navigate(new Uri("/PhonePageOne.xaml", UriKind.Relative));

And in the second retrieve the parameter:

Person p = PhoneApplicationService.Current.State["param"] as Person;

The PhoneApplicationService.State dictionary is a temporary storage location which persists until your app is deactivated.

Other option could be to declare a static member in, for example, App.xaml.cs and use it to save the object from one page and to retrieve from the second one.



回答2:

You can use Messenger --> MVVM Light it's an advanced use for MVVM. Declare a messenger / Register messenger in your view model / Send what you want :) http://mvvmlight.codeplex.com/



回答3:

While the Phone application state method may work, it throws exception when you come back from dormancy. Its not designed to store large objects. A better way to do it would be by overriding the navigation function of navigationservice class. Here is a link to help you guide through it:-

http://www.kunal-chowdhury.com/2013/10/passing-object-to-wp-navigation-service.html