feature: Provide a way to detect when navigation to a view-model is completed, even if no navigation-parameter is being passed
AlexanderMelchers opened this issue ยท 4 comments
Is your feature request related to a problem? Please describe.
The problem I'm currently facing is as follows:
- I'm implementing a detail view for recipes. This detail view can be accessed either from a list that shows all available recipes by directly passing a reference to the recipe to show, or by reference to the identifier of the recipe to show - in which case the recipe will be resolved when the view gets activated.
- I've implemented
WhenNavigatedTo(INavigationParameter)
to pass said data - i.e. the recipe-object or its identifier - to the view-model. - If neither recipe-object or identifier has been specified, I want to inform the user of this fact by showing them an error message.
Now I can show the user an error when INavigationParameter
doesn't contain the expected parameters, but when the view is pushed without INavigationParameter
, WhenNavigatedTo
doesn't get called at all. As far as I'm aware there's no way to detect either 1) whether WhenNavigatedTo
will be called at some point following activation of the view-model; or 2) that WhenNavigatedTo
will not be called at all. That is, other than simply processing WhenNavigatedTo
, I can't predict in the view-model constructor or WhenActivated
whether to wait for WhenNavigatedTo
to occur and have any error messages be handled there, or know that, since WhenNavigatedTo
is not going to be called anyway, I should trigger my error from the constructor or WhenActivated
.
Describe the solution you'd like
Either:
- call
WhenNavigatedTo(INavigationParameter)
withnull
to indicate the lack of parameters having been passed, but the navigation event having taken place; or - implement a separate handler, such as
WhenNavigatedTo()
,WhenNavigatedToWithoutParameter()
orWhenNavigationCompleted(ToOrFrom)
, to indicate that navigation is complete, but without parameters.
Describe alternatives you've considered
Alternatives I've considered are:
- Using the view-model's
WhenActivated
to check if my data has come in. This doesn't work, though, sinceWhenNavigatedTo
consistently occurs afterWhenActivated
. - Consistently passing an empty
NavigationParameter
when I want to trigger the intended error message. This, however, defeats the purpose, as the error is supposed to catch exceptions in the program. - Setting a timer to verify whether data has come in. This, however, may provide false-positives, as the data might need to be retrieved through the network and thus end up taking longer than the time-out that was set.
Describe suggestions on how to achieve the feature
As proposed above, the existing interface and method can be used to achieve this effect by simply always calling the WhenNavigatedTo(INavigationParameter)
-method when a new view-model is navigated to. This, however, may break existing code that either does a null
-check on the incoming parameters, or otherwise simply assumes the parameter to be set. In this case, extending the INavigated
-interface with a new method would solve the issue.
Hey @AlexanderMelchers ๐,
Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our Open Collective and consider contributing financially.
https://opencollective.com/reactiveui
PS.: We offer
priority
support for all financial contributors. Don't forget to addpriority
label once you start contributing ๐
An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms!
Hey @AlexanderMelchers , I was looking the code here and actually what you are requesting can be done already in some way. The WhenNavigatingTo
and WhenNavigatedTo
are called every time a push happens, the only pre-requisite to have that working is to be using the ParameterViewStackService
and call the Push
passing in yourself an empty dictionary as a parameter. If you instead call the Push
methods without the parameter signature (from the ViewStackService
) then indeed those won't be triggered, so I guess that's your case.
Ideally I'd say we should have some kind of special "hook" to have that in a more consise way, there's a feature request I opened for that here #197
I'd say this could be potentially closed and you could keep an eye on the #197 :)
@RLittlesII
@AlexanderMelchers Apologies for not commenting on this sooner. What @winterdouglas says is accurate, the methods get called every navigation and there is nothing preventing you from hooking into them with an empty dictionary. Some base ViewModel class that handles it will get you that information.
We are looking into ways to provide better life cycle methods for the ViewModel. I feel like this is basically the same thing you are requesting. I am going to close this issue as it is similar to the other feature request and solving one, should solve both.