Xamarin TabbedPage 指定的 Cast 无效

Xamarin TabbedPage Specified Cast is not valid

我的 AppShell 中有一个 TabBar,它有五个选项卡。

其中三个选项卡是 ContentPages,而另外两个是 TabbedPages。带有 ContentPages 的所有三个选项卡都可以正常打开,但是带有 TabbedPages 的两个选项卡给我 'Specified cast is not valid' 错误。

AppShell.xaml

<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:local="clr-namespace:ReleaseVerificationAndroid.Views"
       Title="ReleaseVerificationAndroid"
       x:Class="ReleaseVerificationAndroid.AppShell">

    <TabBar x:Name="MainTab" Route="HomePage">
        <Tab Title="Home" Icon="icon_home.png">
            <ShellContent Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}"/>
        </Tab>
        <Tab Title="Fingerprints" Icon="icon_fingerprint.png" IsVisible="True">
            <ShellContent Route="FingerprintPage" ContentTemplate="{DataTemplate local:FingerprintPage}"/>
        </Tab>
        <Tab Title="Mugshots" Icon="icon_mugshot.png" IsVisible="True">
            <ShellContent Route="MugshotPage" ContentTemplate="{DataTemplate local:MugshotPage}"/>
        </Tab>
        <Tab Title="Irises" Icon="icon_iris.png" IsVisible="True">
            <ShellContent Route="IrisPage" ContentTemplate="{DataTemplate local:IrisPage}"/>
        </Tab>
        <Tab Title="Result" Icon="icon_feed.png">
            <ShellContent Route="ResultPage" ContentTemplate="{DataTemplate local:ResultPage}"/>
        </Tab>
    </TabBar>

</Shell>

IrisPage.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"
             x:Class="ReleaseVerificationAndroid.Views.IrisPage"
             Title="{Binding Title}"
             xmlns:local="clr-namespace:ReleaseVerificationAndroid.Views"  
             xmlns:vm="clr-namespace:ReleaseVerificationAndroid.ViewModels">
    <NavigationPage Title="Left Iris" IconImageSource="icon_iris">
        <x:Arguments>
            <local:LeftIrisPage />
        </x:Arguments>
    </NavigationPage>
    <NavigationPage Title="Right Iris" IconImageSource="icon_iris">
        <x:Arguments>
            <local:RightIrisPage />
        </x:Arguments>
    </NavigationPage>
</TabbedPage>

LeftIrisPage 的内容

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:ReleaseVerificationAndroid.ViewModels"
             x:Class="ReleaseVerificationAndroid.Views.LeftIrisPage">
    <ContentPage.BindingContext>
        <vm:LeftIrisViewModel />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="75"/>
                <RowDefinition Height="*" />
                <RowDefinition Height="60"/>
            </Grid.RowDefinitions>
            <ContentView Grid.Row="0" Padding="0,10,0,0" VerticalOptions="FillAndExpand">
                <Image Source="mentalix_logo.png" VerticalOptions="Center" HeightRequest="48" />
            </ContentView>
            <Frame Grid.Row="1" Margin="10,0,10,10" BackgroundColor="Transparent"
                   BorderColor="#333333"
                   CornerRadius="0"
                   HasShadow="True">
                <ContentView VerticalOptions="CenterAndExpand">
                    <Image VerticalOptions="Center" HeightRequest="500" />
                </ContentView>
            </Frame>
            <Grid Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" Margin="10,0,10,12.5" Text="Scan"
                        Command="{Binding RescanLeftIrisCmd}"
                        BackgroundColor="{StaticResource PrimaryButton}"
                        TextColor="White" />

                <Button Grid.Column="1" Margin="2.5,0,10,12.5" Text="Clear"
                        Command="{Binding ClearLeftIrisCmd}"
                        BackgroundColor="{StaticResource Primary}"
                        TextColor="White" />
            </Grid>

        </Grid>
    </ContentPage.Content>
</ContentPage>

由于您的主要目标是嵌套选项卡式页面并同时使用 Shell,您可能会对 Xamarin Community ToolKit 包中的 TabView 感兴趣。现在您可以根据需要嵌套和延迟加载 但目前它仅支持视图 不支持页面。将来它也可能支持页面类型,如果可能,您可以尝试将页面转换为 ContentView

文档 https://docs.microsoft.com/en-us/xamarin/community-toolkit/views/tabview

回购 https://github.com/xamarin/XamarinCommunityToolkit

相关问题 Xamarin forms selected tab top border