生成时的列表视图"Sequence contains no elements"异常



我正在Xamarin Studio(Mac)中构建,并且每次构建时,我都会收到"序列不包含元素"错误。有什么问题?

XAML代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns ="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AJInspector.MenuPage"
Padding="10"
BackgroundColor="Gray"
Title="Inspector Menu">

        <ContentPage.Content>
            <StackLayout Margin="5,30,5,5">
                <Label Text="AInspector Menu">
                </Label>
                <SearchBar Placeholder="search" PlaceholderColor="Gray" HorizontalOptions="Center" SearchButtonPressed="Find_SearchButtonPressed" >
                </SearchBar>
                <Button Text="Inspect" BackgroundColor="Lime" Clicked="FormA_Clicked">
                </Button>
                <Button Text="+ Client Record" BackgroundColor="Silver" Clicked="AddVehicle_Clicked">
                </Button>
            </StackLayout>
            <ListView x:Name="NewVehicles">
                <ListView.ItemTemplate>
                    <DataTemplate>
                       <ViewCell>
                            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                                <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                                    <Label Text="{Binding Make}" ></Label>
                                    <Label Text="{Binding VModel}" ></Label>
                                    <Label Text="{Binding VReg}" ></Label>
                                </StackLayout>
                                    <Button Text="?" HorizontalOptions="EndAndExpand"></Button>
                            </StackLayout>
                       </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ContentPage.Content>
</ContentPage>

代码在MasterDetail页面的主页上实现。ListView的项目库是在页面C#onappearing()方法中设置的。构建适用于Android。

这是我的第一个Xamarin应用,请帮助。

Page中可以有一个一个 Content,在页面的 Content中可以有一个一个子。在那个孩子中,您可以创建嵌套的布局。因此,在您的示例中,如果您想使用StackLayout,最终将使用此结构:

<ContentPage.Content>
    <StackLayout>
        <StackLayout>
        </StackLayout>
        <ListView>
        </ListView>
    </StackLayout>
</ContentPage.Content>

尽管您描述的错误:

序列不包含元素

似乎是关于您的代码中的一个集合,这一事实是在Compile Time上发生的事实告诉您,它必须是您的布局中的东西。否则例外将在运行时发生。

错误仍然与一个集合有关,但是在这种情况下,源自Xamarin内部元素,将您的布局元素放在某种集合中,并且由于您的无效XAML代码而遇到了错误。

如果您尚未启用它,请考虑使用XAML汇编(在此处阅读更多)以在编译时检测这些错误并接收更有意义的错误消息。

最新更新