Navigation with Xamarin Forms default MVVM

3

I recently started studying Xamarin Forms. I have a question that can be clarified here: how to navigate between pages using MVVM? Should your navigation be on vm or view ?

Example

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     x:Class="GeoMobile.Views.Login">
     <StackLayout>
        <Label Text="GEO Mobile" VerticalOptions="Center" HorizontalOptions="Center" />
        <Entry Text="{Binding Email}" Placeholder="Email" />
        <Entry Text="{Binding Senha}" Placeholder="Senha" />
        <Button Text="Entrar" Command="{Binding Entrar}" />
        <Button Text="Registrar" />
     </StackLayout>
</ContentPage>

Login.cs

public partial class Login : ContentPage
{
    public Login()
    {
        InitializeComponent();
        this.BindingContext = new LoginVM(Entrar);
    }

    public async void Entrar()
    {
        Navigation.InsertPageBefore(new Master__(), this);
        await Navigation.PopAsync();
    }
}

And in my vm :

public class LoginVM : ObservableBase
{
    private string _email;

    public string Email
    {
        get { return _email; }
        protected set
        {
            _email = value;
            OnPropertyChanged();
        }
    }

    private string _senha;

    public string Senha
    {
        get { return _senha; }
        set
        {
            _senha = value;
            OnPropertyChanged();
        }
    }

    public Command Entrar { get; private set; }
    private Action loginOk;

    private void login()
    {
        //checagem antes de ir para a proxima pagina
        loginOk();
    }

    public LoginVM(Action loginOk)
    {
        this.loginOk = loginOk;
        Entrar = new Command(login);
    }
}

I got the navigation by passing an action in the constructor of vm, but I need to implement another navigation for the register button, but for that I would already have to pass another action in the constructor and I believe this would not be a good practice.

If you put the navigation in vm, the testing software process will be difficult. What is the best way to get around the problem?

    
asked by anonymous 21.02.2017 / 10:17

2 answers

0

By definition I believe that navigation should be done in your VM, your View should only be used for layout purposes. Of course this is not a rule and if you need to use something in your View for any problem that is occurring in the VM, you will not let your app bug you for it, but by default mvvm I think the most correct would be in your vm .

Generally I create a "service" and in my base view model I use it by dependency injection. It works fine, I've never had any problems.

I recommend this Video by Angelo Belchior, it illustrates well what I wrote above.

    
22.02.2017 / 13:01
0

For those who have the same difficulty as me, I found a great tutorial, it's worth giving a check ...

link

    
22.02.2017 / 06:38