获取异常 Java.Lang.IllegalArgumentException:只能将 TabItem 实例添加到 xamarin 表单中的 TabLayout
Getting exception Java.Lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout in xamarin forms
我遇到异常
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();
//};
}
这是 xaml TabbedPageExampleTab1 的代码
<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 也遵循同样的代码。
这是我需要的UI
我不知道为什么会出现此异常。有什么建议么。
This is the google drive link for the zipped file of the project
使用此代码在 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
。
我遇到异常
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();
//};
}
这是 xaml TabbedPageExampleTab1 的代码
<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 也遵循同样的代码。
这是我需要的UI
我不知道为什么会出现此异常。有什么建议么。 This is the google drive link for the zipped file of the project
使用此代码在 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
。