Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin.Forms Prism: Navigation with buttons not working?

$
0
0

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.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>