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

时间:2021-03-22 13:31:13

标签: xamarin.forms prism

我有一个简单的 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

1 个答案:

答案 0 :(得分:0)

通过 TabbedPages 导航在最新发布的 Prism 版本 (8.0.0.1909) 中被破坏。

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

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

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

相关问题