Quantcast
Viewing latest article 21
Browse Latest Browse All 76418

Why does await _navigationService.NavigateAsync not work in OnNavigatedTo function

Been at this for days. Very simple logic. Jump to Home page if bla = true, skipping the login page.

I've stripped down the code to demo this.
Login button navigates as normal to Home page.
However Login page should actually be skipped as OnNavigatedTo has same Navigate to Home code.

Side affect which makes no sense is HomeViewModel code actually gets run when the OnNavigatedTo function runs. .i.e. ............HomeViewModel Loaded gets written.

public class LoginViewModel : AppMapViewModelBase
{
private readonly INavigationService _navigationService;

public DelegateCommand LoginCommand { get; private set; }

public LoginViewModel(INavigationService navigationService) : base(navigationService)
{
    LoginCommand = new DelegateCommand(LoginUserAsync);
    _navigationService = navigationService;
}


private async void LoginUserAsync()
{
    //This works as expected
    await _navigationService.NavigateAsync("/MasterDetail/NavigationPage/Home");
}


public override async void OnNavigatedTo(INavigationParameters parameters)
{
    base.OnNavigatedTo(parameters);
    //This Executes the code on HomeViewModel but does not Jump to Home.
    await _navigationService.NavigateAsync("/MasterDetail/NavigationPage/Home");
}

}

}

The HomeViewModel code which gets run somehow without ever going to the Home page.

public HomeViewModel(INavigationService navigationService) : base (navigationService)
{
Debug.WriteLine("............HomeViewModel Loaded.................................");
}


Viewing latest article 21
Browse Latest Browse All 76418

Trending Articles