从 TabbedPage 中的选项卡导航到页面

Navigate to a page from a tab within a TabbedPage

我有一个简单的 Xamarin.Forms 应用程序,其主页为 TabbedPage,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
    x:Class="PrismSample.Views.MainPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:prism="http://prismlibrary.com"
    xmlns:views="clr-namespace:PrismSample.Views"
    Title="Main Page"
    prism:ViewModelLocator.AutowireViewModel="True">

    <TabbedPage.Children>
        <NavigationPage Title="Page1">
            <x:Arguments>
                <views:OnePage Title="Page 1" />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="Page2">
            <x:Arguments>
                <views:TwoPage Title="Page 2" />
            </x:Arguments>
        </NavigationPage>

    </TabbedPage.Children>
</TabbedPage>

在“OnePage”页面上我有一个按钮。当我点击该按钮时,我想导航到名为“ThreePage”的页面。

在页面上:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    x:Class="PrismSample.Views.OnePage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:prism="http://prismlibrary.com"
    prism:ViewModelLocator.AutowireViewModel="True">
    <ContentPage.Content>
        <StackLayout Margin="20" Spacing="20">
            <Label HorizontalOptions="CenterAndExpand" Text="Page 1" />
            <Button
                x:Name="Button1"
                Command="{Binding ClickMeCommand}"
                HorizontalOptions="FillAndExpand"
                Text="Open Page 3" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

OnePageViewModel:

namespace PrismSample.ViewModels
{
    public class OnePageViewModel : BindableBase
    {
        private readonly INavigationService navigationService;
        public DelegateCommand ClickMeCommand { private set; get; }

        public OnePageViewModel(INavigationService navigationService)
        {
            this.navigationService = navigationService;
            ClickMeCommand = new DelegateCommand(async () => await ClickedAsync(), () => true);

            Debug.WriteLine($"NavigationStack: {this.navigationService.GetNavigationUriPath()}", nameof(OnePageViewModel));
        }

        public Task ClickedAsync()
        {
            Debug.WriteLine($"You clicked me!", nameof(OnePageViewModel));

            return navigationService.NavigateAsync("ThreePage");
        }
    }
}

App.xaml.cs

namespace PrismSample
{
    public partial class App
    {
        public App() : this(null) { }

        public App(IPlatformInitializer initializer) : base(initializer) { }

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

            var result = await NavigationService.NavigateAsync("MainPage");

            if(!result.Success)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
            containerRegistry.RegisterForNavigation<OnePage, OnePageViewModel>();
            containerRegistry.RegisterForNavigation<TwoPage, TwoPageViewModel>();
            containerRegistry.RegisterForNavigation<ThreePage, ThreePageViewModel>();
        }
    }
}

我预计会出现以下情况: 当点击按钮时,将在第一个选项卡中导航到“ThreePage”页面。我还希望在导航栏中有一个“后退”按钮。

发生了什么: 第一个选项卡中没有任何内容。但是,如果我切换到第二个选项卡,它会显示“ThreePage”页面,但也没有“后退”按钮。

这是怎么回事???

我在这里附上了我的项目: https://www.dropbox.com/s/fuu2wo5zqhp52gk/08-TabbedNavigation.zip?dl=0

在最新的 Prism 发布版本 (8.0.0.1909) 中无法通过 TabbedPages 导航。

[Bug] Navigation when in Tabbed Pages doesn't change currently selected tab #2218

(看来他们终于解决了这个问题,但直到现在他们还没有发布新版本)

所以将 Prism.DryIoc 降级到 7.2.0.1422 或等待下一个 Prism 版本...