I am using Silverlight 4 + PRISM + MVVM in my appli开发者_运维百科cation. I want to pass a complex object to another page. I dont know how to do that. I cant/dont want to use URI parameters.
Maybe EventAggreagtor will be usefull
"Ask for what you want" is the Unity way. You can share objects registered as singletons with any other unity objects. Just specify the interface of that shared object in the constructor of your viewmodels.
As mentioned by Dmitry Kushnier you can also pass complex types as parameters via the EventAggregator.
I implemented INavigationAware interface, and set the complex object in navigationContext.NavigationService.Region.Context in OnNavigatedFrom() method. Now in the next page, again I implemented INavigationAware interface and now I check for the same value in OnNavigatedTo() to get the value.
Update: I posted answer with code in another question. Mentioning it here for reference -
First derive the viewmodel from INavigationAware interface -
public class MyViewModel : INavigationAware
{ ...
You can then implement OnNavigatedFrom and set the object you want to pass as navigation context as follows -
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
SharedData data = new SharedData();
...
navigationContext.NavigationService.Region.Context = data;
}
and when you want to receive the data, add the following piece of code in the second view model -
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
if (navigationContext.NavigationService.Region.Context != null)
{
if (navigationContext.NavigationService.Region.Context is SharedData)
{
SharedData data = (SharedData)navigationContext.NavigationService.Region.Context;
...
}
}
}
hope it helps you all!
精彩评论