Problem with navigation using MVVM in Xamarin

3

I want to navigate using the MVVM pattern in Xamarin.Forms, for that I offer a command to a certain image as seen in the image ...

the code of this view is linked to the MainViewModel and to a property called MenuSisquim located inside the StackLayout

MenuSisquimView.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             BindingContext="{Binding Main, Source={StaticResource Locator}}"
              x:Class="AppValora.Views.Sisquim.MenuSisquimView">

    <ContentPage.Content>

        <StackLayout
          BindingContext="{Binding MenuSisquim}"
            HorizontalOptions="Start" 
            Margin="15">
            <Image Source="ic_sisquim_hds"
                   HeightRequest="130"
                   WidthRequest="130"
                   Margin="23,15">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer
                        Command="{Binding SelectHDSCommand}">
                    </TapGestureRecognizer>
                </Image.GestureRecognizers>
            </Image>

        </StackLayout>

    </ContentPage.Content> 
</ContentPage>

This MenuSisquim property was declared in the MainViewModel as follows:

public MenuSisquimViewModel MenuSisquim { get; set; }

    #region Constructor
     public MainViewModel()
     {
       instance = this;

       Login = new LoginViewModel();
       navigationService = new NavigationService();          
       LoadMenu();
      }
    #endregion

    #region Singleton
     static MainViewModel instance;

  public static MainViewModel GetInstance()
      {
      if (instance == null)
      {
        return new MainViewModel();
      }
      return instance;
      }
    #endregion

I understand that if I am extending the StackLayout to the MenuSisquim Property, the command that displays the event should be implemented in my MenuSisquimViewModel, which I attach the code below

MenuSisquimViewModel.cs:

#region Servicios
public NavigationService navigationService { get; set; }
#endregion


#region Constructor
public MenuSisquimViewModel()
{            
    navigationService = new NavigationService();           
}
#endregion   

#region Comandos
public ICommand SelectHDSCommand
{
    get
    {
        return new RelayCommand(GoHDS);
    }
}

async void GoHDS()
{       
    await navigationService.NavigateOutMaster("FiltrosSisquimView");
}
#endregion

It's where a problem is happening! When I do "Tap" it's not entering the command! , in addition to not having compilation errors, I do not know what is happening! I'm new using the MVVM pattern and I think I have everything in order, I'm using a singleton to see the instances of other ViewModels but I can not execute what I want ... any help for me?

    
asked by Cristofher Ambiado 25.09.2018 в 02:48
source

2 answers

1

Try using the command interface instead relaycommand

    public ICommand SelectHDSCommand
    {
        get { return new Command(GoHDS); }
    }
    
answered by 03.10.2018 в 05:54
0

Everything seems to be in order but you do not show all the files related to the MVVM functionality.

Check this document to make sure you do not miss a step before you get to use the command.

link

    
answered by 01.11.2018 в 20:34