I am using Prism
in my Xamarin.Forms
app and for some reason OnNavigatedTo
is not being called on the other pages in my TabbedPage
. It seems to be because I am using navigation pages for each tab, but I looked through the docs, the Github page, the forums etc and I can't seem to find the correct way to make it work. Has anyone had any luck getting this to work properly?
TabsPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage 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" xmlns:views="clr-namespace:MyApp.Views" xmlns:local="clr-namespace:MyApp;assembly=MyApp" x:Class="MyApp.Views.TabsPage">
<TabbedPage.Children>
<NavigationPage Title="Tab 1">
<x:Arguments>
<views:HomePage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab 2">
<x:Arguments>
<views:SecondPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab 3">
<x:Arguments>
<views:ThirdPage />
</x:Arguments>
</NavigationPage>
<NavigationPage Title="Tab 4">
<x:Arguments>
<views:FourthPage />
</x:Arguments>
</NavigationPage>
</TabbedPage.Children>
</TabbedPage>
TabsPage.xaml.cs:
using Prism.Navigation;
using Xamarin.Forms;
namespace MyApp.Views
{
public partial class TabsPage : TabbedPage, INavigatingAware
{
public TabsPage()
{
InitializeComponent();
}
public void OnNavigatingTo(NavigationParameters parameters)
{
foreach (var child in Children)
{
(child as INavigatingAware)?.OnNavigatingTo(parameters);
(child?.BindingContext as INavigatingAware)?.OnNavigatingTo(parameters);
}
}
}
}
SecondPageViewModel.cs:
using System.Collections.Generic;
using System.Linq;
using Prism.Navigation;
using System.IO;
using MyApp.Models;
using System.Collections.ObjectModel;
using Xamarin.Forms;
using System.Reflection;
namespace MyApp.ViewModels
{
public class SecondPageViewModel : BaseViewModel, INavigationAware
{
public SecondPageViewModel(INavigationService navigationService) : base(navigationService)
{
}
public override void OnNavigatedTo(NavigationParameters parameters)
{
System.Diagnostics.Debug.WriteLine("ON NAVIGATED TO RAN!");
}
}
}
BaseViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using Prism.Navigation;
using Xamarin.Forms;
namespace MyApp.ViewModels
{
public class BaseViewModel : BindableBase, INavigatingAware, IDestructible
{
public INavigationService _navigationService { get; }
public BaseViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
public virtual void Destroy()
{
}
public virtual void OnNavigatedFrom(NavigationParameters parameters)
{
}
public virtual void OnNavigatedTo(NavigationParameters parameters)
{
}
public virtual void OnNavigatingTo(NavigationParameters parameters)
{
}
public Command NavigateCommand
{
get
{
return new Command<string>(async (url) =>
{
await _navigationService.NavigateAsync(url);
});
}
}
public Command BackCommand
{
get
{
return new Command(async () =>
{
await _navigationService.GoBackAsync();
});
}
}
}
}