无法使用 Prism Navigation 在 TabbedPage 中打开特定页面
Can't open specific page in TabbedPage with Prism Navigation
我的应用程序中有一个带有两个选项卡的 TabbedPage,我想使用 NavigateAsync 导航到特定选项卡,但是当我使用 NavigationService.NavigateAsync ("NavigationPage/TabbedPage/SelectedPage") 时,我的应用程序仅打开 SelectedPage堆栈中的 TabbedPage。我可以单击 SelectedPage 中的后退按钮返回到 TabbedPage。
有没有人知道哪里出了问题?
这是我的 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>
这是我在 App 中的 OnInitialized 方法和 RegisterTypes 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");
}
一个好的解决方案是为标签页子项设置一个名称,然后通过NavigationParameters
使用页面名称和所需的标签名称调用NavigateAsync
函数。检查以下示例:
YourTabbedPage
的部分 XAML。
...
<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
需要知道 OnNavigatingTo
何时发生。感谢 Prism,如果页面实现接口 INavigatingAware
,它将能够读取导航参数。
...
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);
}
}
}
...
最后,直接导航到所需的选项卡。
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<YourTabbedPage>();
}
protected override void OnInitialized()
{
InitializeComponent();
NavigationService.NavigateAsync(
"YourTabbedPage",
new NavigationParameters($"tab=tab2")
);
}
...
希望对您有所帮助!
解决方案发布在此 link https://forums.xamarin.com/discussion/comment/330770#Comment_330770
导航的行为在 prism 版本 7 中发生了变化。打开特定选项卡的新行为是
NavigateAsync("TabbedPage?selectedTab=PageName")
我的应用程序中有一个带有两个选项卡的 TabbedPage,我想使用 NavigateAsync 导航到特定选项卡,但是当我使用 NavigationService.NavigateAsync ("NavigationPage/TabbedPage/SelectedPage") 时,我的应用程序仅打开 SelectedPage堆栈中的 TabbedPage。我可以单击 SelectedPage 中的后退按钮返回到 TabbedPage。
有没有人知道哪里出了问题?
这是我的 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>
这是我在 App 中的 OnInitialized 方法和 RegisterTypes 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");
}
一个好的解决方案是为标签页子项设置一个名称,然后通过NavigationParameters
使用页面名称和所需的标签名称调用NavigateAsync
函数。检查以下示例:
YourTabbedPage
的部分 XAML。
...
<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
需要知道 OnNavigatingTo
何时发生。感谢 Prism,如果页面实现接口 INavigatingAware
,它将能够读取导航参数。
...
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);
}
}
}
...
最后,直接导航到所需的选项卡。
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<YourTabbedPage>();
}
protected override void OnInitialized()
{
InitializeComponent();
NavigationService.NavigateAsync(
"YourTabbedPage",
new NavigationParameters($"tab=tab2")
);
}
...
希望对您有所帮助!
解决方案发布在此 link https://forums.xamarin.com/discussion/comment/330770#Comment_330770
导航的行为在 prism 版本 7 中发生了变化。打开特定选项卡的新行为是
NavigateAsync("TabbedPage?selectedTab=PageName")