Can't open specific page in TabbedPage with Prism Navigation

I have a TabbedPage with two tabs in my app and I want use NavigateAsync to navigate to a specif tab, but when I use NavigationService.NavigateAsync ("NavigationPage/TabbedPage/SelectedPage"), my app open only SelectedPage with TabbedPage in the stack. I can click on the back button in SelectedPage to back to the TabbedPage.

Does anyone had any idea about what is wrong?

Here is my TabbedPage axml:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="correct namespace was hide"
            xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
            prism:ViewModelLocator.AutowireViewModel="True"
            x:Class="correct namespace was hide">
    <TabbedPage.Children>
        <local:Pacientes/>
        <local:Sobre/>
    </TabbedPage.Children>
</TabbedPage>

Here is my OnInitialized method and RegisterTypes in App class:

protected override async void OnInitialized()
        {
            InitializeComponent();

            if (Device.RuntimePlatform.Equals(Device.Android))
            {
                await NavigationService.NavigateAsync("Android.Main/OdontoWayPacienteNavigation/Sobre");
            }
            else
            {
                await NavigationService.NavigateAsync("/NavigationPage/iOS.Main/Sobre");
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<OdontoWayPacienteNavigation>();
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<Pacientes>();
            containerRegistry.RegisterForNavigation<Clinicas>();
            containerRegistry.RegisterForNavigation<PacienteEdit>();
            containerRegistry.RegisterForNavigation<ClinicaMap>();
            containerRegistry.RegisterForNavigation<LinkWeb>();
            containerRegistry.RegisterForNavigation<Sobre>();
            containerRegistry.RegisterForNavigation<Views.Android.PacienteAcessos, PacienteAcessosViewModel>("Android.PacienteAcessos");
            containerRegistry.RegisterForNavigation<Views.iOS.PacienteAcessos, PacienteAcessosViewModel>("iOS.PacienteAcessos");
            containerRegistry.RegisterForNavigation<Views.Android.Main, MainViewModel>("Android.Main");
            containerRegistry.RegisterForNavigation<Views.iOS.Main>("iOS.Main");
        }

Upvotes: 0

Views: 860

Answers (2)

The solution was posted at this link https://forums.xamarin.com/discussion/comment/330770#Comment_330770

The behaviour for Navigation changed in version 7 of prism. The new behaviour to open an specific tab is

NavigateAsync("TabbedPage?selectedTab=PageName")

Upvotes: 1

denys-vega
denys-vega

Reputation: 3697

A good solution is to set a name to the tabbed page children, and call the NavigateAsync function using the page name and the desired tab name through NavigationParameters. Check the following example:

Partial XAML of YourTabbedPage.

...
<TabbedPage.Children>
    <ContentPage Title="Tab 1" x:Name="tab1"/>
    <ContentPage Title="Tab 2" x:Name="tab2"/>
    <ContentPage Title="Tab 3" x:Name="tab3"/>
</TabbedPage.Children>
...

YourTabbedPage needs to know when OnNavigatingTo occurs. Thanks to Prism, if the page implements interface INavigatingAware it will be able to read the navigation parameters.

...
public partial class YourTabbedPage : TabbedPage, INavigatingAware
{
    public YourTabbedPage()
    {
        InitializeComponent();
    }

    public void OnNavigatingTo(NavigationParameters parameters)
    {
        if (parameters.TryGetValue("tab", out string tabName) == true)
        {
            SelectedItem = this.FindByName<Page>(tabName);
        }
    }
}
...

Finally, navigate directly to the desired tab.

...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation<YourTabbedPage>();
}

protected override void OnInitialized()
{
    InitializeComponent();
    NavigationService.NavigateAsync(
        "YourTabbedPage", 
        new NavigationParameters($"tab=tab2")
    );
}
...

I hope it helps!

Upvotes: 0

Related Questions