How to navigate between views

In POS application, you can open any number of views in different modes (primary view, modal view, message view), with only one of each group active at a time. The other views are determined as inactive. At any time in any primary view, you can call up the window for switching between primary views (navigation menu) to activate the other selected view that was earlier opened and not closed (it became inactive). The navigation menu can be called up neither in the model view nor in the message view.

Navigation menu

You can press CTRL+TAB or use the  button, available in the status section next to the time icon, to call up the navigation menu. Open views are represented in this window with colored tiles with a title, an icon and/or additional description. You can set a tile title using either the Header property in the View class or the SwitchHeader property in the viewmodel (if you want it to be different from the view heading). An icon and a color are set with the help of styles in the ModernUI.xaml file. Colors and icons are set in exactly the same way as they are set for a registered tile in the main menu. Therefore, if the view was already registered in the main menu, then the tile in the navigation menu window will take on the same color and icon as the tile in the main menu, provided that the value of the resource key (first argument) used in the RegisterMenuTile method is the same as the value of the HeaderLayoutId property in code-behind of the view. In your example, the names are different, which is why you will need to add additional lines in ModernUI.xaml.

Defining tile color and icon in the navigation menu window in ModernUI.xaml for the exemplary OrdersView view:

<SolidColorBrush x:Key="OrdersViewId.Default.Background" Color="Red" /><models:ImageKey x:Key="OrdersViewId.Default.ImageKey" SvgValue="ListIcon" />

Note that OrdersViewId in the above example is the value assigned to HeaderLayoutId in the view class.

In addition, you can add a supplementary description in the tile (as presented in the [Receipt] tile in the above figure). This text is defined with the SwitchHeader2 property in the viewmodel class.

How to open views

Views can be opened in several modes (as primary view, modal view or message view). You can control whether a specific view is to be opened as an independent view from other views (a separate line item in the navigation menu window) or whether it is to be a descendant of the currently open view (no new line item in the navigation menu). When opening a view, you can set the IsPreviewMode parameter (it opens the view in read-only mode, most development objects are automatically edit-blocked) as well as any number of custom parameters.

Views can be opened using the methods available in the ViewModelBase class and in the IViewManager service. The simplest way to open a primary view is to invoke the OpenView<TView>() method, where TView is the name of the class of the view you want to open or activate if it is already opened. Additional parameters:

  • isChild (bool) – specifies whether a view is to be a child view of the currently active one, when opened,
  • parameters (NavigationParameters) – allows passing any number of custom parameters to the viemodel of the view being opened,
  • isPreviewMode (bool) – this parameter is already available in the viewmodel constructor of an opened view (unlike parameters, only available in the viewmodel‘s initialization methods described in Navigation method invoke sequence). It opens a view in preview mode (with limited functionality). Setting it to true will set the read-only property IsPreviewMode in the ViewModelBase class. In addition, the development objects TextBox, SwitchBox, RadioButton, NumericTextBox, ComboBox, CheckBox from the Comarch.POS.Presentation.Core namespace will switch to read-only mode whenever any of them is used in this view.

To open a view in the modal view mode, use the OpenModalView<TView>() method, where TView is the name of the class of the view you want to open or activate if it is already opened. Additional parameters:

  • parameters (NavigationParameters) – similarly as for OpenView
  • isPreviewMode (bool) – similarly as for OpenView

This method does not have the isChild parameter, because all the modal views are opened in the hierarchical mode (they are descendants of the view, which opened it). An exception is the lack of relationship between different view types. For instance, when opening a modal view from an active primary view, a parent-child relationship will not be created.

To open the view in the message view mode, you need to follow two steps. The first one is to set a horizontal alignment view in the xaml:

<core:View x:Class="Comarch.POS.Presentation.Sales.Views.OrdersView"
                      HorizontalAlignment="Stretch"
…

And the second one is to invoke the OpenModalView<TView>() method, available only in the ViewManager service, where TView is the name of the class of the view you want to open. Similarly as in the modal view, a parent-child relationship always excludes the relationships between different view types. Additional parameters:

  • parameters (NavigationParameters) – similarly as for OpenView and OpenModalView,
  • isPreviewMode (bool) – similarly as for OpenView and OpenModalView

If you just want to display a simple message or question with additional buttons, you only need the use the MonitService service. Is provides such methods as:

  • ShowInformation – shows a message view, including any content and the [OK] button,
  • ShowError – shows a message view, including an exception and the [OK] button,
  • ShowQuestion – shows a message view, including any content and the [Yes] and [No] buttons,
  • ShowInformation – shows a message view, including any content as well as any predefined action buttons ([OK], [Yes/No]) or any number of custom buttons.

For more information about messages, see chapter Messages.

How to close views

To close views, you can use the methods for closing a currently active view or selected view (with an option of closing also all its descendants). When closing a view, you can add custom parameters that will be passed to the view to be activated when the current view is closed, similarly as to opening views.

Closing an active view always activates the view from which the view currently being closed was earlier opened. When views are linked via parent-child relationships (when opening IsChild=true view), by closing the child view you will return to the parent view.

To close an active view, use the method as described below.

In the viewmodel, invoke the Close() method which directly invokes the CloseView method in the ViewManager, by passing its view as a parameter. The Close method always closes the view related with the current viewmodel. Additional parameters:

  • parameters (NavigationParameters) – used to pass the information from the view being closed to the view that will be activated

To close a selected view (active or inactive), use the CloseView() method available in the IViewManager service. Additional parameters:

  • view (IView) – specifies which view is to be closed,,
  • closeParents (bool) – specifies whether to also close all dependent views in the parent-child relationship (views opened with IsChild=true parameter). By default, the value to set to true.

During navigation between views (opening, closing, switching an active view), special views are invoked in the viewmodels used in navigation. These methods are used to to execute appropriate actions, depending on whether a view is opened for the first time, reactivated, deactivated or closed. They also provide the parameters that are passed while invoking the opening and closing methods.

Navigation methods are provided by the primary class of each viewmodelViewModelBase. These methods are OnInitialization, OnActivation, OnActivated, OnDeactivted, Dispose, and IsTarget. Relevant methods are invoked automatically during the opening process in both the view from which a new one is being opened and in the target view. During the closing process, specific methods are invoked in the view being closed and in the view that will consequently be activated. The same applies when switching between opened views. An important point is which methods are invoked during a specific navigation and their sequence.

The diagram below presents the process of opening, closing, and switching between views:

Method description:

  • IsTarget (ViewModelBase)

It is invoked while opening a view before opening the view constructor in all instances of viewmodels of the views whose type match the type of the view being opened. If there is any instance of the view of type consistent with the type of the opened view, then a new view will be created. If there are already opened views of this type, then this method will invoke in each viewmodel of the view in the same sequence in which they were opened. If any of them returns true, it will be activated. However, if any of them does not return true, a new instance will be created.

  • OnInitialization (ViewModelBase)

it is invoked in the ViewModel only while opening a new view (new instance, new tab). Access to the parent view is denied (invoking ViewManager.GetParentViewModel(this) will return null).
Note: in this method you should neither open nor close views!

  • OnActivation (ViewModelBase)

It is invoked each time a view opened or activated. Access to the parent view is denied (invoking ViewManager.GetParentViewModel(this) will return null).
Note: in this method you should neither open nor close views!

  • OnActivated (ViewModelBase)

It is invoked each time a view opened or activated.

  • OnDeactivated (ViewModelBase)

It is invoked while deactivating or closing a view.

  • Dispose (ViewModelBase)

It is invoked only when closing a view. It is used to release the resources that are not manageable by Garbage Collector or to stop the descendant threads.

  • OnApplyViewTemplate (View)

It is invoked once in the code-behind of the view class after the view instance is created and after all the view components (development objects) are loaded.

Czy ten artykuł był pomocny?