Home > Software design >  How to close a navigated child view from the main view in MVVMCross?
How to close a navigated child view from the main view in MVVMCross?

Time:06-30

I'am pretty new in MVVMCross and I am using MVVMCross 8.0.2 for a WPF application. Basically what I'am trying to do is to have main view that have mode selections for the application. When i change the mode selection (with buttons) views shown on the main view should change. I used Mvx.Wpf.ItemsPresenter library for this purpose. I used the sample project provided in this answer: sample project

In the main view I created a MvxContainer region inside a border. Two different views share the same region

<Border Grid.Column="3" Grid.ColumnSpan="3" Grid.Row="1" Grid.RowSpan="5" BorderBrush="Blue" BorderThickness="5" >
        <ItemsControl region:MvxContainer.Id="NestedViewRegion"> </ItemsControl>
</Border>

Navigation works as shown in this figure: Image

Red region is the main view, blue region is the child views i am navigating. Problem is that when i navigate with the buttons, both child views are added to the region. What i want to do is to close one of them by using the same buttons which belongs to the main view.

For ex.: when i click navigate 1 button, first child view should appear, after i click the navigate 2 button first child view should close and second child view should appear.

I tried to use MvxPresentationHint to achieve that but i think it can just close the child view within the child viewmodel. Code below shows the navigation commands attached to buttons, this code is in the main viewmodel.

    public async Task FirstNavigateCommand()
    {
        //await _navigationService.ChangePresentation(new MvxClosePresentationHint());
        await _navigationService.Navigate<NestedViewModel>();
    }

    public async Task SecondNavigateCommand()
    {
        //
        await _navigationService.Navigate<NestedNestedViewModel>();
    }

I also tried to use Messenger plugin but couldn't manage to work with navigationservice.

If anyone have any ideas on how to close a navigated view from the main view, i would appreciate the help.

CodePudding user response:

Problem is solved with the help of the owner of the sample project. Solution is using the GUI commands, which are explained in the Mvx.Wpf.ItemsPresenter github repository.

By adding the behaviors namespace and using the interaction triggers with the GUI commands solved my problem. Namespace below should be added to view.

         xmlns:i = "http://schemas.microsoft.com/xaml/behaviors" 

Then interaction triger is added to the button content.

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{x:Static region:MvxWpfPresenter.CloseViewCommand}"
                                          CommandParameter="NestedViewRegion">
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>`

CloseViewCommand is executed when the button is clicked, which closes the last view inside the region.

  • Related