获取异常Java.Lang.IllegalArgumentException:只能将TabItem实例以xamarin形式添加到TabLayout

时间:2018-12-03 06:34:27

标签: xamarin xamarin.forms tabbedpage

我要例外了

Java.Lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout

这是我的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"
            xmlns:pages="clr-namespace:RestaurantApp"
            x:Class="RestaurantApp.SearchTabbedPage">
  <!--Pages can be added as references or inline-->
    <TabbedPage.Children>
        <NavigationPage Title="VENDOR NAME">
            <x:Arguments>
                <pages:TabbedPageExampleTab1 />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="PRODUCT/SERVICE">
            <x:Arguments>
                <pages:TabbedPageExampleTab2 />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

这是我的代码背后的代码

  namespace RestaurantApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class SearchTabbedPage : TabbedPage
    {
        public SearchTabbedPage ()
        {
            InitializeComponent();

        }
    }
}

这是app.xaml.cs代码

 public App()
        {
            InitializeComponent();

            MainPage = new SearchTabbedPage();


            //};
        }

这是TabbedPageExampleTab1的xaml代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="RestaurantApp.TabbedPageExampleTab1">
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Hello. I am Judson" Margin="5" />


        <Label Text="You are on Tab one"></Label>

        <BoxView VerticalOptions="FillAndExpand" Color="Silver"></BoxView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是背后的代码

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TabbedPageExampleTab1 : ContentPage
    {
        public TabbedPageExampleTab1 ()
        {
            InitializeComponent ();
        }
    }

TabbedPageExampleTab2也遵循相同的代码。

这是我需要的用户界面

enter image description here

我不知道为什么我会得到这个例外。有什么建议么。 This is the google drive link for the zipped file of the project

1 个答案:

答案 0 :(得分:1)

使用此代码在TabbedPage中添加页面

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:pages="clr-namespace:RestaurantApp"
            x:Class="RestaurantApp.SearchTabbedPage">
    <TabbedPage.Children>
        <NavigationPage Title="VENDOR NAME">
            <x:Arguments>
                <pages:TabbedPageExampleTab1 />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="Product/Service">
            <x:Arguments>
                <pages:TabbedPageExampleTab2 />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

您的SearchTabbedPage应该为空,表示无需使用此行

Children.Add(new TabbedPageExampleTab1());

编辑:您的Tabbar.xaml文件应如下所示

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabIndicatorColor="@android:color/white"
    app:tabGravity="fill"
    app:tabMode="fixed">    
</android.support.design.widget.TabLayout>

请勿在其中添加TextView