You can use selfVM.Navigate(root, Class.ViewModels.YourTarget) in an action to switch to a ViewModel chosen by an expression when static ViewModel references are not sufficient.
Navigate is an OCL operation for conditional navigation. It lets an action select both the root and the target ViewModel at runtime rather than relying only on the action's static ViewModel reference.
Use the following form:
selfVM.Navigate(root, Class.ViewModels.YourTarget)
| Part | Meaning |
|---|---|
selfVM
|
The current ViewModel context from which the action performs navigation. |
Navigate(...)
|
Switches the active view. |
root
|
The root expression to use for the target view. |
Class.ViewModels.YourTarget
|
The target ViewModel expression. Replace YourTarget with the ViewModel that the action should open.
|
Use Navigate when the target ViewModel or its root must be selected conditionally. For example, an action can evaluate which ViewModel name to use for a particular root, then navigate to that result.
If the action can use a fixed ViewModel reference, use that static reference instead. Static references make the application's navigation easier to inspect and understand.
- Open the action that should change the view.
- Enter an OCL expression using
selfVM.Navigate. - Supply the root expression as the first argument.
- Supply the target ViewModel expression as the second argument.
- Test every condition that can select a different root or target ViewModel.
Example
The following expression navigates from the current ViewModel context to YourTarget using root:
selfVM.Navigate(root, Class.ViewModels.YourTarget)
In this example, replace root with the expression that identifies the object to display and replace YourTarget with the required target ViewModel. When those values vary by condition, put the appropriate expressions in the two arguments.
Use with caution
Dynamic navigation makes it harder to determine statically how your application moves between ViewModels. Keep the conditions that select the root and target ViewModel clear and limited. Treat Navigate as a last resort when static action ViewModel references cannot express the required navigation.
