alansiqueira27
alansiqueira27

Reputation: 8534

How can I pass an object beetween pages in Silverlight?

for example:


NavigationService.Navigate(new Uri("/PlayerLost.xaml", UriKind.Relative));

I don't want to use querystring. I heard about viewmodel... Thank you.

Upvotes: 2

Views: 529

Answers (4)

Arthur
Arthur

Reputation: 1

What I do is very simple:(vb example) In the app.xaml.vb file, open the file and you will see:

Partial Public Class App Inherits Application

just add Partial Public Class App Inherits Application Public Shared wname As String

Then in the page that you want to use the variable, just call app.wname

Done!

Happy coding!

Arthur

Upvotes: 0

Austin Lamb
Austin Lamb

Reputation: 3116

There's no good way to pass an object, and keep in mind the problems that passing an object can result in - if this Frame is browser-integrated (the default, so back/forward buttons in the browser work with your Frame) then it means the URL that will show up in the user's browser will end in "/PlayerLost.xaml"

Then if that URL is copy/pasted (or a favorite is made, etc...) and the user goes back to the page, the object you're wanting to move around won't be there (because it's a fresh app instance).

So you'd be better off using query strings for this sort of state, because then the state is in the URL and the app can get it whether it gets there during normal execution or via a deeplink.

If the data you need to pass is too complex for a query string to represent, then store the object somewhere "global" (adding a property to your Application class is the usual way, or make a static property somewhere) and access it from the resulting page. The page should be resilient to this data not being present (for the deeplink scenario).

Upvotes: 1

Chris Phelps
Chris Phelps

Reputation: 228

Late answer, but I was also trying to find the answer. Hopefully this works for you, but it's in VB. Hopefully it's easy enough decipher. I have a Class called ITRSClass. On my MainPage.xaml.vb, I have:

Public Shared myClass As New ITRSClass

this could also be any object or variable for example:

Public Shared myString as New String = "My New String"

then, from the new page that I open (in my case called Setup.xaml.vb), I can get a reference to that object or variable like this:

Dim mySharedClass = MainPage.myClass

or:

Dim mySharedString = MainPage.myString

In my case, I have an ObservableCollection of a class that I use to keep a ListBox on my MainPage updated, based on results from my (Setup.xaml.vb) page.

Here is where I learned this - much better explanation: Dave Corun's Blog

Upvotes: 2

danbord
danbord

Reputation: 4096

Instead of using the "Navigate" you can first create your page manually and set the Content property of the ContentFrame. Using that method you can expose a public property to your object and set it before loading it into the ContentFrame :

Views.MyView vw = new Views.MyView();
vw.MyParameter = ... ;
this.ContentFrame1.Content = vw;

Or you can use global variables, but use them wisely :)

Upvotes: 1

Related Questions