【问题标题】:"Sequence contains no elements" error“序列不包含元素”错误
【发布时间】:2017-11-21 18:32:28
【问题描述】:

我以简单的 xaml 形式收到错误“序列不包含元素”。 我对 Xamarin 表单还很陌生,所以请多多包涵。

有什么想法吗?

<?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="CRM.Views.CustomerItem" Title="Customer Info">

<ContentPage.Content>

    <StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Text="Name" Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding CustName}" Grid.Row="0" Grid.Column="1"/>
            <Label Text="Surname" Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding CustSurname}" Grid.Row="1" Grid.Column="1"/>
            <Label Text="Address" Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding Address}" Grid.Row="2" Grid.Column="1"/>
            <Label Text="PhoneNumber" Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding PhoneNumber}" Grid.Row="3" Grid.Column="1"/>
        </Grid>
    </StackLayout>

    <Button Text="Save" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Clicked="Save_Clicked"></Button>
    <Button Text="Cancel" HorizontalOptions="FillAndExpand" BackgroundColor="Red" TextColor="White" Clicked="Cancel_Clicked"></Button>

</ContentPage.Content>

【问题讨论】:

  • 您的按钮不包含在任何布局中,并且您的网格没有定义任何行

标签: c# xaml xamarin mobile xamarin.forms


【解决方案1】:

始终确保 ContentPage.Content 只有一个布局控件,如 StackLayout 或 Grid 等,以及其中的所有其他控件。

<ContentPage.Content>
   <StackLayout>
       <!-- all controls go here -->
   </StackLayout>
</ContentPage.Content>

这将解决问题

【讨论】:

    【解决方案2】:

    按钮很好,但网格没有定义行,但您使用的是 Grid.Row="0"。由于 Grid.Rows 中没有行,它说“序列不包含元素”

    尝试添加行定义

    <StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
    
        <Grid>
            <Grid.RowDefinitions>
                 <RowDefinition Height="100" />
                 <RowDefinition Height="100" />
                 <RowDefinition Height="100" />
                 <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Text="Name" Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding CustName}" Grid.Row="0" Grid.Column="1"/>
            <Label Text="Surname" Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding CustSurname}" Grid.Row="1" Grid.Column="1"/>
            <Label Text="Address" Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding Address}" Grid.Row="2" Grid.Column="1"/>
            <Label Text="PhoneNumber" Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center"></Label>
            <Entry Text="{Binding PhoneNumber}" Grid.Row="3" Grid.Column="1"/>
        </Grid>
    </StackLayout>
    
    <Button Text="Save" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Clicked="Save_Clicked"></Button>
    <Button Text="Cancel" HorizontalOptions="FillAndExpand" BackgroundColor="Red" TextColor="White" Clicked="Cancel_Clicked"></Button>
    

    【讨论】:

      【解决方案3】:
      You Must be Create Rows and Columns Both...
      
      <Grid>
            <Grid.RowDefinitions>
              <RowDefinition Height="2*" />
              <RowDefinition Height="*" />
              <RowDefinition Height="200" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
          </Grid>
      

      【讨论】:

        【解决方案4】:

        Jason 是对的。有多个错误:

        1. Content 只需要一个 View 元素。

        在这种情况下,这应该是StackLayout。您的按钮 SaveCancel 应该在 StackLayout 内。

        1. 您的Grid 不包含任何RowDefinition

        如果您只有一个元素并且没有定义行,这将起作用。由于您明确使用多行,因此您需要一个RowDefiniton

        所以你的情况应该是这样的

        <?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="CRM.Views.CustomerItem" Title="Customer Info">
        
        <ContentPage.Content>
        
            <StackLayout Padding="10" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        
                <Grid>
                    <Grid.RowDefinitions>
                         <RowDefinition Height="Auto" />
                         <RowDefinition Height="Auto" />
                         <RowDefinition Height="Auto" />
                         <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
        
                    <Label Text="Name" Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center" />
                    <Entry Text="{Binding CustName}" Grid.Row="0" Grid.Column="1" />
                    <Label Text="Surname" Grid.Row="1" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center" />
                    <Entry Text="{Binding CustSurname}" Grid.Row="1" Grid.Column="1" />
                    <Label Text="Address" Grid.Row="2" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center" />
                    <Entry Text="{Binding Address}" Grid.Row="2" Grid.Column="1" />
                    <Label Text="PhoneNumber" Grid.Row="3" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Center" />
                    <Entry Text="{Binding PhoneNumber}" Grid.Row="3" Grid.Column="1" />
                </Grid>
        
                <Button Text="Save" HorizontalOptions="FillAndExpand" BackgroundColor="Blue" TextColor="White" Clicked="Save_Clicked" />
                <Button Text="Cancel" HorizontalOptions="FillAndExpand" BackgroundColor="Red" TextColor="White" Clicked="Cancel_Clicked" />
            </StackLayout>
        
        </ContentPage.Content>
        

        【讨论】:

          【解决方案5】:

          对此的简单答案是将所有控件包含在一个面板中。下面的一个将显示您将如何放置。

           <ContentPage.Content>
                  <StackLayout>
          
                      <StackLayout Padding="40" Margin="0,80,0,0">
                          <Label Text="UserName" TextColor="Black" ></Label>
                          <Entry Text="" Placeholder="UserName" x:Name="username"></Entry>
                          <Label Text="Password" TextColor="Black"></Label>
                          <Entry Text="" Placeholder="Password" IsPassword="True" ></Entry>
                          <Button Text="Login" Clicked="Login_Clicked"></Button>
                          <Label Text="Not a Member? sign up Now" TextColor="Black" HorizontalOptions="Center"></Label>
          
                      </StackLayout>
                  </StackLayout>
              </ContentPage.Content>
          

          如果你把任何东西放在堆栈面板之外,就会给你那个错误。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多