【发布时间】: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