Xamarin.Forms Prism: Navigation with buttons not working?

GyllionGyllion NLMember ✭✭

Hello everyone,

I think I've found a strange bug in Prism.Forms. I'm trying to navigate to a new view using a button. The button in the first view navigates just fine. The button in the second view doesn't.

Here's the code for the first view:

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="BindingButtonTest.Views.MainPage"
             Title="MainPage">
  <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
    <Button Text="To Second View" Command="{Binding SecondViewCommand}"/>
  </StackLayout>
</ContentPage>

ViewModel:

public class MainPageViewModel : BindableBase, INavigationAware
{
    private readonly INavigationService _navigationService;

    public DelegateCommand SecondViewCommand { get; set; }


    public MainPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
        SecondViewCommand = new DelegateCommand(StartSecondView);
    }

    private void StartSecondView()
    {
        _navigationService.NavigateAsync("SecondView");
    }

    public void OnNavigatedFrom(NavigationParameters parameters)
    {

    }

    public void OnNavigatingTo(NavigationParameters parameters)
    {

    }

    public void OnNavigatedTo(NavigationParameters parameters)
    {

    }
}

Here's the code for the second view:

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="BindingButtonTest.Views.SecondView">
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <Button Text="To Third View" Command="{Binding ThirdViewCommand}"/>
    </StackLayout>
</ContentPage>

ViewModel:

 public class SecondViewViewModel : BindableBase, INavigationAware
 {
    private readonly INavigationService _navigationService;

    public DelegateCommand ThirdViewCommand { get; set; }


    public SecondViewViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
        ThirdViewCommand = new DelegateCommand(StartThirdView);
    }

    private void StartThirdView()
    {
        _navigationService.NavigateAsync("ThirdView");
    }

    public void OnNavigatedFrom(NavigationParameters parameters)
    {

    }

    public void OnNavigatingTo(NavigationParameters parameters)
    {

    }

    public void OnNavigatedTo(NavigationParameters parameters)
    {

    }
 }

I'm using Prism version 6.3.0 and Xamarin.Forms version 2.4.0.38779.

Best Answer

Answers

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    Make sure you have registered your pages for navigation

  • GyllionGyllion NLMember ✭✭

    @BrianLagunas:

    I did. Here's my app.xaml.cs:

     public partial class App : PrismApplication
     {
        public App(IPlatformInitializer initializer = null) : base(initializer) { }
    
        protected override void OnInitialized()
        {
            InitializeComponent();
    
            NavigationService.NavigateAsync("NavigationPage/MainPage?title=Hello%20from%20Xamarin.Forms");
        }
    
        protected override void RegisterTypes()
        {
            Container.RegisterTypeForNavigation<NavigationPage>();
            Container.RegisterTypeForNavigation<MainPage>();
            Container.RegisterTypeForNavigation<SecondView>();
            Container.RegisterTypeForNavigation<ThirdView>();
        }
    }
    
  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    What makes you think it is not navigating? Do you get an exception? Upload your sample so I can see what is going wrong.

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭
    edited November 2017

    Oh wait... I see the problem. You're not navigating because your BindingContext is not set to an instance of a VM. Your command is not even firing since you're not bound. When you have a "View" in your page name, the VM name should be just SecondViewModel. Not SecondViewViewModel.

  • GyllionGyllion NLMember ✭✭

    @BrianLagunas:

    No, there is no exception. The command 'StartThirdView' is just never reached.
    Here's the project:

    https://github.com/GyllionVanElderen/BindingButtonTest

  • GyllionGyllion NLMember ✭✭

    @BrianLagunas:

    I changed the 'View' naming to 'Page'. It still doesn't work. I pushed it to the repo

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    Works perfect! Delete your obj and bins folders and uninstall the app from your emulator

  • BrianLagunasBrianLagunas USInsider ✭✭✭✭

    Works perfect! Delete your obj and bins folders and uninstall the app from your emulator

  • GyllionGyllion NLMember ✭✭

    @BrianLagunas:

    Unfortunately, it doesn't. I'm not able to navigate to the third page when I press the 'To Third Page' button, it doesn't navigate to the third page at all. Does this work for you? I can only see in the image that you were able to navigate to the second page, not the third.

  • GyllionGyllion NLMember ✭✭

    @BrianLagunas That makes a lot of sense, thanks! It works now as it should be:

  • capitolcapitol Member ✭✭

    I don't see it. Did you edit your original post??

Sign In or Register to comment.