I'm having problems understanding how Master Detail navigation works in Xamarin Forms, let's go to my code as it is now:
1- I created a interface
for navigation:
using System.Threading.Tasks;
namespace MobileApp.Services
{
public interface INavigationService
{
Task NavigateToLogin();
Task NavigateToRegister();
Task NavigateToMain();
Task NavigateToCoupons();
Task NavigateToProfile();
}
}
2 - I created the classe
that implements the interface
:
using MobileApp.Views;
using System.Threading.Tasks;
using System;
namespace MobileAPP.Services
{
public class NavigationService : INavigationService
{
public async Task NavigateToCoupons()
{
await App.Current.MainPage.Navigation.PushAsync(new CouponsView());
}
public async Task NavigateToLogin()
{
await App.Current.MainPage.Navigation.PushAsync(new LoginView());
}
public async Task NavigateToMain()
{
await App.Current.MainPage.Navigation.PushAsync(new MainView());
}
public async Task NavigateToRegister()
{
await App.Current.MainPage.Navigation.PushAsync(new RegisterView());
}
public async Task NavigateToProfile()
{
await App.Current.MainPage.Navigation.PushAsync(new ProfileView());
}
}
}
3- This is my MainView
that inherits from MasterDetailPage
and sets it to master
and detail
.
using Xamarin.Forms;
namespace MobileApp.Views
{
public partial class MainView : MasterDetailPage
{
public MainView()
{
InitializeComponent();
this.Master = new MasterView();
this.Detail = new NavigationPage(new DetailView());
}
}
}
4- My home screen is view
of Login
, and I only have método
where I check my credentials. Login ok, I already use interface
of navigation to MainView
and it works perfectly, at this point I have no problem. Follow código
:
private async void Login()
{
var IsAuthenticated = await _authenticate.Login(Email, Password);
if (!string.IsNullOrEmpty(IsAuthenticated))
{
await this._navigationService.NavigateToMain();
}
else
{
await this._messageService.ShowAsync("Email ou senha invalidos");
}
}
5- The problem occurs in MasterView
, which is the side menu. In it I have several Button
that lead to other pages, it follows código
of MasterViewModel
:
using MobileApp.Services;
using System.Windows.Input;
using Xamarin.Forms;
using System;
namespace MobileApp.ViewModels
{
class MasterViewModel : ViewModelBase
{
private readonly IMessageService _messageService;
private readonly INavigationService _navigationService;
public ICommand CouponsCommand { get; set; }
public ICommand ProfileCommand { get; set; }
public MasterViewModel()
{
this.CouponsCommand = new Command(this.Coupons);
this.ProfileCommand = new Command(this.Profile);
}
private async void Profile()
{
await this._navigationService.NavigateToProfile();
}
private async void Coupons()
{
await this._navigationService.NavigateToCoupons();
}
}
}
When I try to navigate, the error occurs: Object reference not set to an instance of an object.
when clicking on any button.
But as you can see in the implementation of interface
of navigation I'm instanciando a página
, just like it happens after Login
which leads to MainView
and works fine.
If I remove the código
that uses the interface
_navigationService
and put the same código
that is in classe
NavigationService
in Profile
, for example:
private async void Profile()
{
await App.Current.MainPage.Navigation.PushAsync(new ProfileView());
}
private async void Coupons()
{
await this._navigationService.NavigateToCoupons();
}
So the navigation works, I really did not understand what's going wrong. An observation; I'm not using any framework
to MVVM
(type mvvmcross, prismmvvm, etc) because first I want to learn how it all works. Can anyone explain where the error is?