【问题标题】:Getting exception Java.Lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout in xamarin forms获取异常 Java.Lang.IllegalArgumentException:只能将 TabItem 实例添加到 xamarin 表单中的 TabLayout
【发布时间】:2018-12-03 06:34:26
【问题描述】:

我遇到了异常

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 也遵循相同的代码。

这是我需要的用户界面

我不知道为什么会出现此异常。有什么建议。 This is the google drive link for the zipped file of the project

【问题讨论】:

  • 我认为这里Children.Add(new ContentPage());,你应该添加ContentPage 类型如Page1 或EmployePage 等而不是直接ConentPage
  • 是的,我也试过了
  • 我将编辑代码背后的代码
  • 您需要在 TabbedPage 中添加多少页,能否请您提及有问题的名称,例如我需要添加 page1、page2 等。
  • @CGPA6.4 我已经参考了 TabbedPageExampleTab1 但它仍然给了我同样的例外。

标签: xamarin xamarin.forms tabbedpage


【解决方案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。

【讨论】:

  • 您不应在SearchTabbedPage.cs 中再次添加 Tab。您的页面TabbedPageExampleTab1 或TabbedPageExampleTab2 应与ContentPage 一起继承。
  • 是的,我也这样做了。我现在就放上我的最新代码。
  • 底部标签是 TabbedPageExampleTab1 和 TabbedPageExampleTab2 如您所见
  • 并且我已将选项卡式页面设置为初始页面,正如您在我的 app.xaml.cs 代码中看到的那样
  • 如果可能,请提供我的项目 zip 存储库。这样我就可以检查到底发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 1970-01-01
  • 2019-10-02
相关资源
最近更新 更多