0
votes

I have a xamarin forms app using Prism, I'm using a control template in App.xaml having a icon, which on tap should go to the previous page. In the tapGesture event method imgBack_Tapped , I'm calling NavigationService.GoBackAsync(); but it doesn't go to the previous page.

Brian xamarin forums link says here to avoid using NavigationService apart from in viewmodels, but then I would have to not use a control Template.

Any solution or alternative would be great, thanks in advance.

App.xaml

<ControlTemplate x:Key="TitleBarTemplate">
    <Grid Grid.RowSpacing="0">
      <Grid.RowDefinitions>
        <RowDefinition Height="0.1*" />
        <RowDefinition Height="0.9*" />
      </Grid.RowDefinitions>

      <StackLayout Grid.Row="0" BackgroundColor="#2ea5d5" Orientation="Horizontal" Padding="10,0">
        <Image Source="Back.png" WidthRequest="30" HorizontalOptions="Start" IsVisible="{TemplateBinding Parent.IsBackBtnVisible}">
          <Image.GestureRecognizers>
            <TapGestureRecognizer Tapped="imgBack_Tapped"/>
          </Image.GestureRecognizers>
        </Image>
        <Label Text="{TemplateBinding Parent.TitleBarText}" TextColor="White" FontSize="18" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Style="{StaticResource TitleBarFont}"></Label>
        <Image Source="logo_round.png" WidthRequest="50" HorizontalOptions="End" IsVisible="{TemplateBinding Parent.IsRoundLogoVisible}"></Image>
      </StackLayout>

      <ContentPresenter Grid.Row="1">

      </ContentPresenter>
    </Grid>
  </ControlTemplate>

App.xaml.cs

 protected override void OnInitialized()
    {
        InitializeComponent();
        NavigationService.NavigateAsync("NavigationPage/Login");
        NavigationPage.SetHasNavigationBar(this, false);
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<NavigationPage>();
        Container.RegisterTypeForNavigation<Login>();
        Container.RegisterTypeForNavigation<Dashboard>();
        Container.RegisterTypeForNavigation<MyActivity>();
        Container.RegisterTypeForNavigation<About>();
    }

    private async void imgBack_Tapped(object sender, EventArgs e)
    {
        try
        {
            await NavigationService.GoBackAsync();
        }
        catch (Exception ex)
        {

        }
    }

UPDATE : I was able to solve it , I created a command in my view model and made the binding context of the image tapped command to refer to the view model.

App.xaml

<Image Source="Back.png" WidthRequest="30" HorizontalOptions="Start" IsVisible="{TemplateBinding Parent.IsBackBtnVisible}">
          <Image.GestureRecognizers>
            <TapGestureRecognizer Command="{TemplateBinding Parent.BindingContext.BackImgTappedCommand}"/>
          </Image.GestureRecognizers>
        </Image>

And in my viewmodel SamplePageViewModel

private async void BackImgTapped()
    {
        await _navigationService.GoBackAsync(); 
    }
1

1 Answers

0
votes

Try to receive the navigationService in your viewModel constructor:

private ICommand navigatePackagesCommand;

        public ICommand NavigatePackagesCommand
        {
            get { return navigatePackagesCommand; }
        }

public YourPageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
 //...
}

and use this to navigate:

_navigationService.NavigateAsync("DestinationPage");

or in your case:

_navigationService.GoBackAsync();