“序列不包含任何元素”错误

时间:2017-06-19 13:44:56

标签: c# xaml xamarin mobile xamarin.forms

我以简单的xaml形式获得错误“Sequence contains no elements”。 我对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>

5 个答案:

答案 0 :(得分:8)

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

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

这将解决问题

答案 1 :(得分:1)

Buttons are fine but the grid has no rows defined but you are using Grid.Row="0". As there are no rows in the Grid.Rows it says "Sequence contains no elements"

try adding row definition

<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>

答案 2 :(得分:0)

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>

答案 3 :(得分:0)

Jason是对的。有多个错误:

  1. Content只占用一个View元素。
  2. 在这种情况下,这应该是StackLayout。您的按钮保存取消应位于StackLayout范围内。

    1. 您的Grid不包含任何RowDefinition
    2. 如果您只有一个元素而没有定义行,那么这将起作用。由于您明确使用多行,因此需要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>
      

答案 4 :(得分:0)

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

 <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>

如果您在堆栈面板之外放置任何东西,都会给您带来错误。

相关问题