Passing state between pages?

2019-02-27 11:49发布

问题:

I have a Silverlight Windows Phone 7 app with two pages. I want to pass some nontrivial state (an array or structs) between them. Do I have to follow a Web model where everything needs to be packed into query string? Makes little sense when all pages and classes are on the same device, within the same process and assembly.

So the questions are:
- upon navigation between pages, is there a good way to pass data as-is?
- upon page navigation, does code-behind of the source page have access to the code-behind of the destination page (or vice versa)?
- is there any shared user object that all pages can refer to (like an ASP session)?

Alternatively, is there a way to nest XAML's? I could make do with a model where there's a outer container page that loads different content pages into a panel on it.

回答1:

You have a couple of options:

  1. Use the query string as you suggested. This makes sense from a web development perspective, but we aren't in a web development perspective.
  2. Use a static variable. This is probably the simplest. Just declare another class with a static property, and you can share data this way. The only concern with this approach is thread-safety.
  3. Use Isolated Storage.


回答2:

No you don't have to pass everything in a query string. Just pass an id and store the data non-trivial data in isolated storage between pages.

There is a series of articles on how to to do this here