How to communicate between the Views in the Silverlight 3.0 application?
I am building a Silverlight 3.0 app based on the Silverlight Navigation Application template. One 开发者_开发问答road block I ran into is communicating between the Pages. For instance, I am in one Page, and I want to kick off another page and send it some data. I am at a loss as to how to do this.
Any ideas?
You have 2 options
- Use Publish/Subscribe pattern, i havent used this before, but it is useful in certain cases, i dont think it would solve ur problem though.
Use Request parameters, basically when you navigate to the new view, call it like so
NavigationService.Navigate(new Uri("/HomePage.xaml?HomePageId=12", UriKind.Relative));
You could slap some string.format in there to make it neater, but you get the idea, then on the new View, use string queryParam = HtmlPage.Document.QueryString["HomePageId"];
on page load to get the Id of the information you wish to display.
That should do it.
I've found an effective solution that does not involve too much complexity. I am using the LocalMessageSender and LocalMessageReceiver objects. The original purpose of these objects are to have 2 silverlight apps on the same web page talk to each, but it's just as good at having 2 views talk to each other.
The usage is really straight-forward. See this example for usage.
Jesse Liberty has an excellent blog post about just this. I use the techniques described there myself in a multi page Silverlight application.
I'm not sure though whether you can apply this with the Silverlight Navigation Application template.
精彩评论