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?
Use a query string value:-
MainPagecan then acquire this value using:-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:-
You would then implement this interface and expose the implementation as singleton say:-
Your OpenPage calls
MessageService.Default.Storeand 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.Retrievewhich removes the item from the service.