views:

1132

answers:

2

I have a Page in Silverlight which is Navigated from the MainPage.xaml, called OpenPage.xaml, I then want to pass a value back to the MainPage.xaml - this OpenPage.xaml is called using this:

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

From the mainpage - this is not a child of the MainPage as the RootVisual is replaced - I can call this:

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

To return to the MainPage - however I need to pass a value from OpenPage.xaml to the MainPage.xaml - how to I access the MainPage instance - I have a Property called Document however when I do this:

        MainPage m = (MainPage)Application.Current.RootVisual;
        m.Document = "Hello World";

Or this:

((MainPage)root).Document = "Hello World";

I get an invalid cast exception because I think it is trying to cast the OpenPage.xaml to MainPage.xaml - how to I get the NavigatedTo Page, I want to set the property on MainPage.xaml from OpenPage.xaml.
I also want to pass values from the MainPage.xaml to another page SavePage.xaml - but this has the same issue - how do I resolve this?

+2  A: 

Use a query string value:-

NavigationService.Navigate(new Uri("/MainPage.xaml?value=Hello%20World", UriKind.Relative);

MainPage can then acquire this value using:-

string value =  this.NavigationContext.QueryString["value"];

Edit:

In response to comment re passing other types.

Once you have the above inplace you can use other service patterns to pass other types. For example consider a MessageService that implements:-

interface IMessageService
{
    Guid Store(object value)
    object Retrieve(Guid key)
}

You would then implement this interface and expose the implementation as singleton say:-

public class MessageService : IMessageService
{
    public static IMessageService Default { // singleton stuff here }
}

Your OpenPage calls MessageService.Default.Store and places the resulting Guid in the query string.

The MainPage then tests for the presence of such a query string value, if present uses its value to call MessageService.Default.Retrieve which removes the item from the service.

AnthonyWJones
This is an interesting solution - like ASP.NET QueryStrings, this may be fine for my current example, but is it possble to do it with objects as this may be useful for more complex data interaction between pages. Currently I need it for a filename for isolated storage, but my need it for other uses later. Thanks.
RoguePlanetoid
As an additional where and how best to read the QueryString value that has been passed in.
RoguePlanetoid
@RoguePlanetoid: How is what I thought I've already shown, where? Probably in the OnNavigatedTo override would be the best.
AnthonyWJones
Thanks - reading it the first way works fine - but the second is also very useful for more complex examples - this is actually for a tutorial I was working on - will post the link when it is done. Thanks again!
RoguePlanetoid
A: 

We've got a more advanced Silverlight Navigation Framework that allows you to elegantly pass data during navigation.

For instance, you can do this using our UXFrame:

frame.Navigate(new Uri("/MainPage", UriKind.Relative), yourExtraData));

Later, in the OnNavigatedTo method, you can retrieve the data through:

object yourExtraData = e.ExtraData;

One more thing is that this API is compatible with WPF counterpart, so if you copy-paste this code to a WPF project, it'll run just as is.

Check out http://intersoftpt.wordpress.com/2010/06/29/clientui-part-5-the-supercharged-silverlight-navigation/

Hope this helps.

James