I have a simple questions why on these two platforms my code is working in different way. In first step I am sending a message, using command binded to the button in UI from MainViewModel
.
private RelayCommand<Set> _setCommand;
public RelayCommand<Set> SetCommand
{
get
{
return _setCommand
?? (_setCommand = new RelayCommand<Set>(
set =>
{
_navigationService.Navigate("QuestionView", set);
Messenger.Default.Send<Set, QuestionViewModel>(set);
}));
}
}
In the second step I register message in constructor in QuestionViewModel
like this:
Messenger.Default.Register<Set>(this, Load);
In this solution I use PCL to sharing the code between platforms.
In the third step I would like to fire Load method, simple:
private void Load(Set set)
{
Load(set, 1);
}
And in the Windows Store project everything works as I expected, but in the Windows Phone project the Load
method is firied not at the first time, only at second. So I have to go to QuestionView
- nothing, go back and enter again and now everything is working. Load
method is fired. What is the problem?
Best, Tomasz
The ViewModel
behind QuestionView
is probably not instantiated yet and can therefore not receive messages. Only after going to QuestionView
the ViewModel
gets instantiated (by the ViewModelLocator
). If you want to send messages to the QuestionViewModel
you should instantiate it immediately in the ViewModelLocator
like this:
SimpleIoc.Default.Register<QuestionViewModel>(true);
I Hope this helps.
Here is the solution I found usefull: ViewModelLocator
#if NETFX_CORE //WINDOWS 8
SimpleIoc.Default.Register<QuestionViewModel>();
#else //WINDOWS_PHONE
SimpleIoc.Default.Register<QuestionViewModel>(true);
#endif
and registering services in App.xaml.cs
like this:
public App()
{
SimpleIoc.Default.Register<IQuestionService, QuestionService>();
}
This solution is pretty nice no code-behind in Views and all functionalities of ViewModels are stored in PCL
project.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.