Reputation: 562
I'm am new to the .NET/c# realm and I am trying to develop a Windows Phone 8 application.
I have several pages showing lists of Objects(ListPage
). All of these pages will have a filter capability, using a commom FilterPage
.
What I need is to pass an object from the ListPage
to the FilterPage
.
I want to use MVVM (MVVM light templates). I've managed to implement almost everything using the Messaging framework. I'm using a FilterMessage
that takes the object to be passed in its constructor.
The ListPage
and the FilterViewModel
listen to this message. The ListPage
will navigate to the FilterPage
and The FilterViewModel
will take the Object from the message.
The ListPage
is notified correctly but the FilterViewModel
is not notified because the FilterViewModel
is created only after the FilterPage
is first shown.
Is it possible to initialize the FilterViewModel by App start? If you think that this is not the way to go please tell me why:)
Thanks in advance.
Upvotes: 1
Views: 976
Reputation: 562
I managed to make it work by creating the FilterViewModel
instance in the ViewModelLocator
constructor.I'm not sure that this is the best way to do it. Hopefully someone with more experience will share with us his thoughts.
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<FilterViewModel>();
ServiceLocator.Current.GetInstance<FilterViewModel>();
}
Upvotes: 1
Reputation: 2001
What I do in this scenario is I'm adding the instance you want to pass around to the other page into the Session object you have under PhoneApplicationService.Current.State.
On the other side after you complete navigation you can extract the instance, and you should remove it from the State.
Pay attention that if your application goes into the background when you have instances inside the State, WP will try to serialize them.
Upvotes: 1